Answer to Question #185304 in C++ for Chathu

Question #185304

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)


1
Expert's answer
2021-04-28T02:48:54-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS