CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2009
    Posts
    11

    Exclamation 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. #2
    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. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

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

    How do they not work?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  6. #6
    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. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by saritadubey View Post
    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. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile 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. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by itsmeandnobodyelse View Post
    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. #10
    Join Date
    Oct 2009
    Posts
    577

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

    Quote Originally Posted by GCDEF View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured