Thread: printing only prime numbers

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    printing only prime numbers

    I have one more problem:
    I need to displey number of prime numbers. Number is enetered from user.
    I need to use only while loops.
    I tried, but having problems-infinite loop

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int n,i=1,prime=1;
    	printf("How many prime numbers do you want: ");
    	scanf("%d",&n);
    	while(n)
    	{
    		i=1;
    		while(i<=prime)
    		{
    			if(prime%i==0)
    				break;
    			i++;
    		}
    		if(prime==i)
    		{
    			printf(" %d",prime);
    			n--;
    			
    
    		}
    		prime++;
    					
    	
    	}
    	return 0;
    }
    Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You need to start i at 2 not 1. All integers numbers are divisible by 1 so the case of (prime %i ==0) will always be true.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks!
    I really cannot believe I didn't see this.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int n,i=1,prime=1;
    	printf("How many prime numbers do you want: ");
    	scanf("%d",&n);
    	while(n)
    	{
    		
    		while(i<=prime)
    		{
    			if(prime%i==0)
    			{
    				
    				break;
    			}
    			i++;
    		}
    		if(prime==i)
    		{
    			printf(" %d",prime);
    			n--;
    			
    
    		}
    		prime++;
    		i=2;
    					
    	
    	}
    	return 0;
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You should initalize i to 2 during declaration also.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Well, in that case it will not be displayed 1

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1 is not a prime number. The first prime is 2.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    22
    First look up the definition of Prime number . It says a prime number is one which is divisible by one and the number itself.It also must not be divisible by any other number. So 1 wont be a prime. 2 will be a prime .

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well by the definiton you provided, 1 is divisible by 1 and itself and isn't divisible by any other number, so it would be a prime. It's classed as a special case (i.e. "Well there's no real reason why and it's difficult to express so we'll just do it to be awkward"), isn't it?

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    A prime number (or prime integer, often simply called a "prime" for short) is a positive integer p > 1 that has no positive integer divisors other than 1 and p itself.
    1 is excluded from the definition. Here is my backup.
    Math World

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Or, if you're too lazy to read linuxdude's link, here.
    More concisely, a prime number p is a positive integer having exactly one positive divisor other than 1. (emphasis added)
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I can't believe all these replies about such minor issue as is 1 prime number or not.
    Anyway my homework was to print 1,2,..etc. So I was supposed to write program that will print 1 and others. Thanks for help.
    I would like to see similar discussion on my dijkstra algorithm problem but...
    Last edited by Micko; 06-10-2004 at 08:33 AM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can't believe all this threads about such minor issue as is 1 prime number or not.
    And you've been hanging around here long enough to have over 100 posts?

    >So I was supposed to write program that will print 1 and others.
    Code:
    printf("%d ", 1);
    print_primes();
    My best code is written with the delete key.

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Let me have a go at it

    Quote Originally Posted by Micko
    I can't believe all these replies about such minor issue as is 1 prime number or not.
    Anyway my homework was to print 1,2,..etc. So I was supposed to write program that will print 1 and others. Thanks for help.
    I would like to see similar discussion on my dijkstra algorithm problem but...
    Unfortunetly, in some cases, this community is very helpful and we thrive on helping each other in what ways we can. even when ppl are just trying to get us to do their homework. some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
    Code:
    #include <stdio.h>
    int main()
    {
    	int n,i=1,prime=1;
    	printf("How many prime numbers do you want: ");
        scanf("%d",&n);
        
        /*always check scanf's return value. this case check to make sure n > 0 and is
        an integer. type a letter when first run this program to see why */
        
    	while(n)
    	
       /*as long as n is not zero the code will run so unless user enters
    	'0' code will run accordingly better to put while ( (n > 1) ),theoretically;
        but you already cause an infinite with the next statement */
        
    	{
     	    i=1;
          /*already declared i as 1 why declared again? 
            this will make sure i is ALWAYS 1 */
            
    		while(i<=prime)
      /* prime is declared as 1 so saying while 1 is <= 1 */
    		{
    			if(prime%i==0){/* if the remainder of 1 / 1 == 0  break; this is true as there 
                is no remainder it evaluates to 0*/
                
    			printf("\nprime is now %d, i is now %d: ", prime, i);
    			printf("Press [enter] to continue your code: ");
    			getchar();
                /*this is a test statement. it'll show you the program state. take it out for
                poop and giggles */
                
    			break;
               /* a break statement in a loop causes the program to break of that loop that
    			encloses it and go to the next stage of the program. In this case if prime == i print
    			prime */
    			}
    			i++;
                /* this doesn't matter anyway cause you put i back to 1 again */
    		}
    		if(prime==i)
    		{
    			printf("%d\n",prime);
             /* this will only print once and n will only decrease once
                another infinite loop :d you need to read up on loops and rememebr when in doubt
                paretheses */
    			n--;
    			
    
    		}
    		prime++;/*prime will always increase but i won't because: you have this statement : */
    		i=2;
       /* why even have this assignement cause you declare it back to 1 after so i 
               will forever be 1*/
    					
    	
    	}
    	printf("\nfinished doing what u told me.\nnotice anything strange?\n");
    	system("pause");
    	return 0;
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
    But those of us that could help the most by posting a complete solution to homework satisfy our "intellectual curiosity" and don't post it. Doing someone else's homework does more harm than good.

    ><snip code>
    Tabs and spaces don't mix well. It's usually better if you only use spaces and format your comments and mix them with code to be easier to read. As it is I find your program uncomfortable.
    My best code is written with the delete key.

  15. #15
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Prelude
    >some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
    But those of us that could help the most by posting a complete solution to homework satisfy our "intellectual curiosity" and don't post it. Doing someone else's homework does more harm than good.
    I don't think I solved it. I was merely showing the individual a way to think in order to come to a satisfactory conclusion. or the expected results.

    ><snip code>
    Tabs and spaces don't mix well. It's usually better if you only use spaces and format your comments and mix them with code to be easier to read. As it is I find your program uncomfortable.
    don't be afraid of the code, it won't touch you inapropiately or anything. actaully i was trying to find the vb code tags to format it better, but i couldn't.

    In anycase the issue is that this individual needs help on how to go about getting the desired solution. If s/he pastes the code and run it - which they will have to do - the code will not only be easier to read but s/he can also check the program state, which is more important.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Numbers
    By Ahmed29 in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2008, 10:54 AM
  2. Checking very large prime numbers
    By password in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2008, 12:26 PM
  3. Prime Numbers
    By EdSquareCat in forum C++ Programming
    Replies: 9
    Last Post: 05-29-2007, 05:54 PM
  4. prime numbers code compile error
    By Tony654321 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 10:13 AM
  5. prime numbers
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 08-20-2002, 08:57 PM