Display prime numbers between 1 to 50 using Nested Loops
I need help as I am not able to underdstand where am I making a mistake in the following program to display prime numbers between 1 to 50
Code:
#include<iostream>
class prime
{
int a,b;
public:
void display()
{
b=2;
for (a=4;a<50;a++)
{
while (b<=a/2)
{
if(a%b==0)
{
b++;
break;
}
else if(b==a/2 && a%b!=0)
{
cout<<a;
cout<<",";
}
else
{
b++;
break;
}
b++;
}
}
}
};
void main()
{
prime prime50;
prime50.display();
return 0;
}
Re: Display prime numbers between 1 to 50 using Nested Loops
I also wrote the following program for the same but both of them dont work.
Code:
#include<iostream>
class prime
{
int a,b;
public:
void display()
{
b=2;
for (a=4;a<50;a++)
{
while (b<=a/2)
{
if(a%b==0)
{
b++;
break;
}
else if(b==a/2 && a%b!=0)
{
cout<<a;
cout<<",";
}
else
{
b++;
break;
}
b++;
}
}
}
};
void main()
{
prime prime50;
prime50.display();
return 0;
}
Re: Display prime numbers between 1 to 50 using Nested Loops
Re: Display prime numbers between 1 to 50 using Nested Loops
Time to learn to use the debugger. The problems are obvious if you watch it run and see what it's doing.
What does everyone have against whitespace these days?
Re: Display prime numbers between 1 to 50 using Nested Loops
Is there a specific reason, why you use a while loop for the inner loop as opposed to a for loop?
;);)
Re: Display prime numbers between 1 to 50 using Nested Loops
will you please send me code to write a program in php using class to check prime numbers..the input is given by user..
thank you
Re: Display prime numbers between 1 to 50 using Nested Loops
Quote:
Originally Posted by
saritadubey
will you please send me code to write a program in php using class to check prime numbers..the input is given by user..
thank you
That's funny.
Re: Display prime numbers between 1 to 50 using Nested Loops
In your first code there are some flaws:
(1) the b = 2; must be moved into the for loop and above while loop
(2) remove the whole else block of the inner while loop.
the (1) means that the b wasn't reset for all a > 4 and therefore the inner loop couldn't work.
the (2) breaks the while loop whenever none of the first conditions was true. That is for each odd number.
Regards, Alex
Re: Display prime numbers between 1 to 50 using Nested Loops
Quote:
Originally Posted by
itsmeandnobodyelse
In your first code there are some flaws:
(1) the b = 2; must be moved into the for loop and above while loop
(2) remove the whole else block of the inner while loop.
the (1) means that the b wasn't reset for all a > 4 and therefore the inner loop couldn't work.
the (2) breaks the while loop whenever none of the first conditions was true. That is for each odd number.
Regards, Alex
This is an old thread bumped by a noob looking for somebody to do some homework for him.
Re: Display prime numbers between 1 to 50 using Nested Loops
Quote:
Originally Posted by
GCDEF
This is an old thread bumped by a noob looking for somebody to do some homework for him.
Thanks for the info.
The code posted was not so good that it necessarily must have being copied from somewhere. And it wasn't so bad that it couldn't be repaired following the advise given ... But of course if the real goal was full PHP code, it is still a long way to go ;-)
Regards, Alex