CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Display prime numbers between 1 to 50 using Nested Loops

1. Junior Member
Join Date
Feb 2009
Posts
11

## 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;
}```

2. Junior Member
Join Date
Feb 2009
Posts
11

## 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;
}```

3. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,739

## Re: Display prime numbers between 1 to 50 using Nested Loops

How do they not work?

4. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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 10:17 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?

6. Junior Member
Join Date
Aug 2010
Posts
1

## 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

7. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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.

8. Join Date
Oct 2009
Posts
577

## 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

9. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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.

10. Join Date
Oct 2009
Posts
577

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width