-
February 11th, 2009, 07:31 AM
#1
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;
}
-
February 11th, 2009, 07:38 AM
#2
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;
}
-
February 11th, 2009, 11:08 AM
#3
Re: Display prime numbers between 1 to 50 using Nested Loops
-
February 11th, 2009, 11:15 AM
#4
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?
Last edited by GCDEF; February 11th, 2009 at 11:17 AM.
-
February 11th, 2009, 11:18 AM
#5
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?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
August 2nd, 2010, 02:27 AM
#6
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
-
August 2nd, 2010, 07:25 AM
#7
Re: Display prime numbers between 1 to 50 using Nested Loops
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.
-
August 2nd, 2010, 09:25 AM
#8
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
-
August 2nd, 2010, 09:27 AM
#9
Re: Display prime numbers between 1 to 50 using Nested Loops
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.
-
August 2nd, 2010, 11:35 AM
#10
Re: Display prime numbers between 1 to 50 using Nested Loops
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|