I made a program that will print out all prime numbers up to a certain number the user desires. For instance, the user wants prime numbers up to ten, the program will print out prime numbers up to ten. My problem is that i'm not getting an output on a screen and believe that my break has something to do with that. I'm assuming my break would exit my second while loop.
//Function printing out all the prime numbers.
//Userrangenumber: Parameter, suppose it is 10. This function will print out all of the primenumbers to ten.
void primenumber (int userrangenumber)
{
//Startingnumber: Starts from 2, since it is a first primenumber.
int startingnumber=2;
//Starting number will be divided by the counter.
int counter=1;
//Remainder will be used to see if its prime or not
int remainder;
//***************************************NESTED WHILE LOOP STRUCTURE*********************************************************************
//startingnumber is 2. If userwants the primenumber upto ten.
while (startingnumber<=userrangenumber)
{
//counter will be set to one each time the loop repeats
counter=1;
//Second While looop. Starting number will be divided by the counter. Counter will be incremented until it reaches to starting number.
while (counter<=startingnumber)
{
remainder=startingnumber%counter;
//If remainder is equal to zero, get out of this while loop. First check if starting number is equal to the counter. If
//starting number is equal to the counter, it will be a prime number. If not get out of this while loop using break.
//Increment starting number. You will use the next number to see if its prime.
if (remainder==0)
{
{
if (startingnumber==counter)
cout<< "Prime: " <<startingnumber;
}
break;
}
counter++;
}
startingnumber++;
}
}
On line 26 you divide the startingnumber by counter which will always result in remainder being 0 because every number is evenly divisible by 1.
Therefore, line 30 will always be true the first iteration through the inner loop and line 33 will always be false, so there will never be any output generated. Not because of the break, but because the logic is wrong.
//Parameter userrange number is going to be used to print out prime numbers to that number
void primenumber (int userrangenumber)
{
//You will check each number if theyre are divisible by any number. starting from 1;
int startingnumber=2;
// counter is declared to divide each number by anoter number
int counter;
int remainder;
while (startingnumber<=userrangenumber)
{
counter=1;
while (counter<=startingnumber)
{
remainder=startingnumber%counter;
{
//If remainder is equal to zero, get out of the loop
if (remainder==0)
{
if (startingnumber==counter)
cout<<"\nPrime: " <<startingnumber;
}
counter++;
}
}
startingnumber++;
}
}
ok cire, I tweaked it a litte bit, but still cant find the solution for this. I definitely need help on this now. I was this thinking to add an else statement with anoter counter ++, but im not totally lost now how to solve that mystery one that would always have a remainder of zero.
number = 10;
for(i = 2; i<=number; i++)
{
prime=0;
for(x = 1; x<i; x++){
if(i%x==0){
prime++;
}
}
if(prime<=1){
it is Prime
}
else{
it is not Prime
}
}