The loop shown below has been written by a c++ programmer
int n =1; //A
while (n>=1){
if(condition){ //B
//C
}
n--;
}
1)Update the value of the variable n to repeat the loop 10 times.
2)Write the condition Statement to select only odd numbers
3)Rewrite the code with the statement n++ (You can only add or modify statement A,B,C)
#include <iostream>
using namespace std;
int main()
{
// 1
int n = 10; //A
while (n >= 1)
{
cout << n << endl;
//if (condition) //B
{
//C
}
n--;
}
cout << endl;
// 2
n = 10; //A
while (n >= 1)
{
if (n%2 == 1) //B
{
cout << n << endl; //C
}
n--;
}
cout << endl;
// 3
n = 1; //A
while (n <= 10)
{
if (n % 2 == 1) //B
{
cout << n << endl; //C
}
n++;
}
return 0;
}
Comments
Leave a comment