While loop problem

the loop is not repeated as i need. this program is to print the primary number starting from the number entered by user,but it print one time only if the entered number is primary number. i don't know the reason. please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include<iostream>
using namespace std;

int main()
{

  char again;
  do  //this loop is to ask user if he needs to try again after calculation.
  {

		int x;
		cout<<"please enter number\n";
		cin>>x;  //get the number from user
	

		while(x<=2)   //test the number if it is less than 2.
		{
			cout<<"please enter more than 2"<<endl;
			cin>>x;
		}

	 int i;
	 int z;
	 bool isprim=true;
	 z=x;

	while(z>2) //to test descending.
	{

	for(i=x-1;i>1;i--) //to test every number divided by the previous nu. 
	 {

		if(x%i==0)
		{
		  isprim=false;
		  break;
		}
		
	 }

	 if(isprim==true)
	 {
		 cout<<x<<endl;
	 }

	 x=z;
	 x--;
	 z--; 
	}       //while loop should repeat at this point if z>2.
	cout<<"tryagain??\n press 'y' for yes or any key for exit"<<endl;
	cin>> again;

  }while(again=='y');

 return (0);
}
Which "while" loop isn't repeating?

You have 1 "do-while" loop, lines 8 - 53, and two "while" loops, lines 16-20 and lines 27-49. (Your indentation formatting needs a bit of work)

I copy'n'paste the code, compile (no errors) and run, and get the following output:
please enter number
3
3
tryagain??
 press 'y' for yes or any key for exit
y
please enter number
9
tryagain??
 press 'y' for yes or any key for exit
y
please enter number
7
7
tryagain??
 press 'y' for yes or any key for exit
n

Your program's output could be a bit more user friendly, but the code works for me. No problems as you describe.
Last edited on
Hi Furry Guy

Thanks for reply.

line number 27 this while loop with for loop should test the primary number and print it

thanks
semsemdiver
Printing out the entered number again if it is prime does happen. Look at my test run. 3 and 7 did print out, while 9 didn't.

Changing line 43 to something like:
std::cout << x << " is a prime number.\n";
would help make the output more visible and user-friendly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>

int main()
{
   char again { };

   do  //this loop is to ask user if he needs to try again after calculation.
   {
      int x { };
      std::cout << "please enter number: ";
      std::cin >> x;  //get the number from user

      while (x <= 2)   //test the number if it is less than 2.
      {
         std::cout << "please enter more than 2\n";
         std::cin >> x;
      }

      int z { x };
      bool isprim { true };

      while (z > 2) //to test descending.
      {
         for (int i = x - 1; i > 1; i--) //to test every number divided by the previous nu.
         {
            if (x % i == 0)
            {
               isprim = false;
               break;
            }
         }

         if (isprim == true)
         {
            std::cout << x << " is a prime number.\n";

            // if the number is prime, why continue doing calculations?
            // with or without the break, the output happens only once
            break;
         }

         x = z;
         x--;
         z--;
      }       //while loop should repeat at this point if z>2.

      std::cout << "try again?? press 'y' for yes or any key for exit: ";
      std::cin >> again;
   }
   while (again == 'y');
}

please enter number: 18
try again?? press 'y' for yes or any key for exit: y
please enter number: 127
127 is a prime number.
try again?? press 'y' for yes or any key for exit: n


Hi Furry Guy


the output shouldn't be 127 only, it should be all primary numbers starting from 127 to 2 this is the idea of this program, for some reason the while loop line 22 doesn't repeat??

Thank you for your help,
semsemdiver
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

int main()
{
   char again { };

   do
   {
      int low { 2 };

      std::cout << "Enter number: ";
      int high { };
      std::cin >> high;
      std::cout << '\n';

      std::cout << "Prime numbers in reverse order are:\n";

      // go from high to low
      for (int loop { high }; loop >= low; loop--)
      {
         // perform a "standard" test for "is this number prime?"
         bool isPrime { true };

         for (int i { 2 }; i <= loop / 2; i++)
         {
            if (loop % i == 0)
            {
               isPrime = false;
               break;
            }
         }

         if (isPrime) { std::cout << loop << ' '; }
      }
      std::cout << "\n\n";

      std::cout << "Do you want another number to check if prime? (y or n): ";
      std::cin >> again;
   }
   while (again == 'y' || again == 'Y');
}

Enter number: 127

Prime numbers in reverse order are:
127 113 109 107 103 101 97 89 83 79 73 71 67 61 59 53 47 43 41 37 31 29 23 19 17 13 11 7 5 3 2

Do you want another number to check if prime? (y or n): y
Enter number: 20

Prime numbers in reverse order are:
19 17 13 11 7 5 3 2

Do you want another number to check if prime? (y or n): b

Ooops! I forgot the check if the user wants to enter another number! Added!

If you want low to high primes, change line 14 to:
for (int loop { low }; loop <= high; loop++)
Last edited on
hi
Furry Guy


Thanks for your help.

but i want to know why doesn't my code work? as you see know errors and logically i think it is correct, so what is the problem?

anyway thank you

semsemdiver
If you want/need while or do/while loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

int main()
{
   char again { };

   do
   {
      int low { 2 };

      std::cout << "Enter number: ";
      int high { };
      std::cin >> high;
      std::cout << '\n';

      std::cout << "Prime numbers in reverse order are:\n";

      // go from high to low
      int loop { high };

      do
      {
         bool isPrime { true };

         int i { 2 };

         while (i <= loop / 2)
         {
            if (loop % i == 0)
            {
               isPrime = false;
               break;
            }
            i++;
         }
         if (isPrime) { std::cout << loop << ' '; }

         loop--;
      }
      while (loop >= low);

      std::cout << "\n\n";

      std::cout << "Do you want another number to check if prime? (y or n): ";
      std::cin >> again;
   }
   while (again == 'y' || again == 'Y');
}
Last edited on
logically i think it is correct

Compare your code to mine with the while/do-while loops. You are overthinking.

As was I originally when looking at your code.

I broke the problem down into easy to achieve steps:

1. what's the code for finding a prime number. The code I could remember off-hand used a for loop.

2. Ask the user for the high number. The low number is already set (2), but having it as a variable makes it a quick change if you want a different low number from the user.

3. Loop from high to low. FOR LOOP!

4. slap the code to find a single prime number into the for loop and BINGO!

5. Change the for loops into while/do-while loops.

Assignment done! After adding the code to ask the user if they want to enter another number.
Last edited on
Thank you
Furry Guy


finally i found the reason, i forgot to turn the bool variable to true again before entering the for loop. now my code works :)

thanks furry and please talking to you.
Topic archived. No new replies allowed.