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.
#include<iostream>
usingnamespace 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);
}
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.
#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
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??
#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++)
#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');
}
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.