determine if numbers are prime

This is a discussion on determine if numbers are prime within the C Programming forums, part of the General Programming Boards category; i have to write a program that inputs a number and determines whether the number is prime. After displaying the ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Question determine if numbers are prime

    i have to write a program that inputs a number and determines whether the number is prime. After displaying the results, ask the user if they want to try again. At the end, print Have a nice day!

    The problem Im having is that it only tells if a number is prime the first time, when i try it more times it just says not prime for any number, i would like to see if any of you can tell me what im doing wrong.

    [
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
        
        int a,c=1,i,number;
        char answer;
        do
        {
            {
        printf("Enter number:");
        scanf("%d", &number);
        getchar();
        for(i=1;i<=number;i++)
       {
         a=number%i;
         if(a==0)
            {
               c=c+1;
            }
        }
       if (c==3)
    
        printf(" PRIME NUMBER\n");  
       else
            printf(" NOT PRIME NUMBER\n");
        
        }
        
        printf("Do you want to continue?(y/n)");
        scanf("%c", &answer);
        getchar();
        } while (tolower(answer) == 'y');
        {while(answer == 'n') 
        {
        printf("Have a good day!\n"); break;
        }
        }
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What is the value of c the second time it enters the do... while loop?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    To determine if a number is prime, you have to check if it is divisable by 2 or any previous prime number. So all you have to do is store a list of the prime numbers you've found so far and check if anyone divides your value. The only problem with that approach is that you have to calculate those prime numbers every time you run the procedure.

    EDIT: A solution is to precalcutate all prime numbers ( the ones that fit in an int ), store them in a static array and iterate that array untill you find the passed value. If that value isn't found, it isn't prime.
    EDIT^2: If to the above solution you check in each iteration if the passed value is smaller that the current prime, you won't have to traverse the entire array to find it out.
    Last edited by GReaper; 02-13-2011 at 06:16 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Hello mr Royaro..
    there is no need to write that much code.... simpli we can write like
    // just am giving logic

    main()
    {
    int i,x,n,y,c=0;
    char ch;
    // just am writing pf() instead of printf("") just edit it

    lable: // when you press your choice y compiler comes here and strarts compilation from
    // here
    pf(enter no'r x);
    sf(read value of x);
    for(i=1;i<=x;i++)
    {
    n=x%i;
    if(n==0)
    y++;
    }
    if(c==2)
    pf(prime n'r);
    else
    pf(not pime);
    pf(do u want to continue Y/N);
    ch=getchar();
    if(ch=='y')
    goto lable;
    else
    printf("hav a nic.....");
    getch();
    }


    i hope that it may helps you...!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    KmsReady... Please learn how to use code tags.

    << !! Posting Code? Read this First !! >>

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmsreddy View Post
    Hello mr Royaro..
    there is no need to write that much code.... simpli we can write like
    1) Lose the goto crap. Use a do while loop instead... this isn't BASIC.
    2) There is no reason to check every value when doing this function...
    ..... a) We already know 1 and 2 are prime numbers.
    ..... b) We already know that an even number cannot be prime.

    Code:
    for( i = 3;i <= x; i += 2 )  // go twice as fast by checking only odd numbers.
      {
         n=x%i;
         if(n==0)
         y++;
      }

  7. #7
    Registered User
    Join Date
    Feb 2011
    Location
    New Delhi
    Posts
    5
    Hi,

    I have made changes to your code...hope it helps...

    Cheers !!

    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
        float a=0;
        int c=1,i,number;
        char answer, mprime;
        do
        {
            
        printf("Enter number:");
        scanf("%d", &number);
        getchar();
        
    	mprime = 'Y';
    
    	for(i=1;i<=number;i++)
    		{
    			a=number%i;
    			if ((a==0) && ( i!=number) && (i!=1))
    			{
    
    				printf(" NOT PRIME NUMBER\n");
    				mprime = 'N';
    				break;
    			}
    		}
    	if (mprime =='Y')
    
    		printf(" PRIME NUMBER\n");
    
        
          
        printf("Do you want to continue?(y/n)");
        scanf("%c", &answer);
        getchar();
        } while (tolower(answer) == 'y');
        
    	{while(answer == 'n') 
        {
        printf("Have a good day!\n"); break;
        }
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 08:56 AM
  2. Replies: 11
    Last Post: 04-08-2010, 06:36 PM
  3. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  4. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 03:12 PM
  5. prime numbers
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 08-20-2002, 08:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21