I understand the structure of a do while; however, when I start plugging in my statements, I am not gettng the result I want.
The logic is wrong. Knowing the general form of the do while is:

do
statements
while (condition)

and wanting to write a do while code to find out if a number is prime I entered this into the complier:

int input_number2 = 0; //user input number for do-while loop
int mod_number2 = 0;  ////modulus number for do-while loop
int ctr_number = 0;	////counts factor
int i = 0;	

do
{

cout << \nPlease enter a number";
cin >> input_number2;

	ctr_number = 0;
	i= input_number2;
	i++;

	

}

while(mod_number2 = i%1);

{
	if(mod_number2<=2)
	{
	cout <<"\nYour number is prime";
	}
	else{
(mod_number2>2);

cout <<"\nYour number is not prime";
}
}
return 0;
}

Well it does not work. I am not sure of what to do with the counter. I was able to understand it while writing this code as a for loop; but, not as a do while. Any advise is always appreciated.

Recommended Answers

All 4 Replies

It may be because you have a semicolon at the end of this line:

while(mod_number2 = i%1);

Try removing that. Also, please use [ CODE ] tags around your code - what you posted was very difficult to read.

Dave

Code tags. this has not been introduced to me. Please explain.

write
[ code ]
YOUR CODE
[ /code ]

but without the spaces between the brackets and the word code. This will make it show up nice and organized like it did in my reply.

one more thing, when I take the semi-colon out the code will not complie. Does not mean there is an error somewhere else?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.