Search the Q&A Archives

# write a program to find out whether a given number is prime...

 Question by urvi Submitted on 1/1/2004 Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List) Rating: Rate this question: N/A Worst Weak OK Good Great write a program to find out whether a given number is prime or not.

 Answer by golla.satyanarayana Submitted on 3/17/2004 Rating: Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=0,i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=1;i<=n;i++)    {      a=n%i;      if(a=0)         {            c=c+1;         }     } if (c=2)   { printf("the given number is prime"); } else     printf("the given number is not prime"); }

 Answer by Abhilash Submitted on 1/28/2005 Rating: Rate this answer: N/A Worst Weak OK Good Great #include #include void main() { int no,i; clrscr(); printf(" Enter the Number U want to check:"); scanf("%d",&no); i=2; while(i<=no-1) { if(no%i==0)   {     printf(" %d is not Prime number",no );     break;   } i=i+1; } if(no==i)   printf("%d is a prime Number",no); getch(); }

 Answer by james Submitted on 2/9/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int n,i,a; printf("enter the number"); scanf("%d",&n); if(n==1) printf("it is neither prime nor composite"); for(i=2;i<=n;i++) { a=n%i; if(a==0) { printf("the number is not a prime number"); } else { printf("the number is prime"); } } }

 Answer by naveen Submitted on 5/19/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great void main() { int i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=2;i<=n;i++)    {      if(n%i==0)      printf("The number is prime number") }        else    printf("the given number is not a prime number"); getch(); }

 Answer by goli Submitted on 2/16/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int n, i; printf ("Enter any number \n"); scanf("%d", &n); for (i=2 ; i <=n/2 ; i++)    {      if(n % i ==0)      {         printf(" Its not a prime number\n");           break;      }    }    printf("Its a prime number\n "); }

 Answer by romeo Submitted on 2/20/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include #include #define TRUE 1 #define FALSE 0 int prime(int number) {    int i=2,flg=TRUE,c;    while(i

 Answer by supriya Submitted on 3/29/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=0,i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=1;i<=sqrt(n);i++)    {      a=n%i;      if(a=0)         {            c=c+1;         }     } if (c=2)   { printf("the given number is prime"); } else     printf("the given number is not prime"); }

 Answer by raihana66 Submitted on 5/16/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great int n,n1; print("enter number"); scanf("%i",&n); if(n%2=0&&n1=2) printf("prime number"); else printf("not primenumber);

 Answer by raihana66 Submitted on 5/16/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great int n,n1; printf("enter number"); scanf("%i",&n); if(n%2=1&&n1=2) printf("prime number"); else printf("not prime number);

 Answer by raihana66 Submitted on 5/16/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great int n,c); printf("enter number"); scanf("%i",&n); if(n%2=0&&n>=1&&c=2) printf("prime number"); else printf("not prime number);

 Answer by raihana Submitted on 5/16/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c,i,n; printf("enter the number to be checked"); scanf("%d",&n); if(i=1&&i<=n&&i++)           a=n%i;      if(a=0)         {            c=c+1;         }     } if (c=2)    printf("the given number is prime");   else   printf("the given number is not prime"); }

 Answer by SATINDRA MOHAN Submitted on 7/19/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=1,i,n; printf(" NUMBER = "); scanf("%d",&n); for(i=1;i<=n;i++)    {      a=n%i;      if(a==0)         {            c=c+1;         }     }    if (c==3)     printf(" PRIME NUMBER");       else     printf(" NOT PRIME NUMBER"); getch(); }

 Answer by priya Submitted on 7/23/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great 4 IS NOT A PRIME

 Answer by Sudhakar Desaboina Submitted on 8/23/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=0,i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=1;i<=n;i++)    {      a=n%i;      if(a==0)         {            c=c+1;         }     } if (c==2)     printf("the given number is prime"); else     printf("the given number is not prime"); }

 Answer by Santosh Submitted on 8/31/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great // program for finding prime number #include int isPrime(int n) {    int i,j;    i=1;    j=0;    while(i<=n)    {       if(n%i==0)             j++;       i++;    }    if(j==2)       {       printf("Prime");       return 1;    }    else    {       printf("Not Prime");       return 1;    } } int main() {    int num;    printf("Enter Number : ");    scanf("%d",&num);    isPrime(num);    return 1; }

 Answer by sumanth Submitted on 11/10/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=0,i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=1;i<=n;i++)    {      a=n%i;      if(a==0)         {            c=c+1;         }     } if (c==2)   { printf("the given number is prime"); } else     printf("the given number is not prime"); }

 Answer by neeraj Submitted on 11/11/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() { int a,c=0,i,n; printf("enter the number to be checked"); scanf("%d",&n); for(i=1;i<=n;i++)    {      a=n%i;      if(a==0)         {            c=c+1;         }     } if (c==2)   { printf("the given number is prime"); } else     printf("the given number is not prime"); }

 Answer by sundar Submitted on 12/29/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include main () {   int i, j, prime = 0;   for (i = 1; i < 1000; i++)     {       prime = 0;       for (j = 2; j < (i / 2) + 1; j++)         {           if (i % j == 0)             {               prime = 1;               break;             }         }       if (prime == 0)         {           printf ("%d is a prime number\n", i);         }     } }

 Answer by Shiwani Submitted on 1/14/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great main() {   int i,n,rem,sq;      printf("enter the number to be checked");   scanf("%d"&n);   sq=sqrt(n);   for(i=2,i<=sq,i++)do   {      rem=n%i;      if(rem==0)      {        printf("given number is not prime");        exit;      }   }   printf("given number is prime"); }

 Answer by Arjun Submitted on 1/23/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great int main () { int r,n; printf("Enter a number to be checked : "); scanf("%d",&n); for(r=2;r<=n;r++) { if(n%r==0) { if(r < n) { printf("It is not a prime number\n"); break; } else printf("It is a prime number\n"); } } }

 Answer by fearfact_53 Submitted on 2/9/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great This program runs, if you enter a add number that is not prime it will tell you that it is a prime number. Any way nice try. Email me if you want 1 that works. ( fearfact_53@yahoo.com)

 Answer by KDW Submitted on 2/19/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great // This program determines the nearest prime numbers // Written in C++ // By: KDW // Febuary 1, 2007 // Several revisions are included in this edition /*         1. As soon as a remainder of 0 is encountered the         program moves on to the next incremented value above         or below the seed.         2. When testing whether a particular seed is a prime         the program only test up to 1/2 of the seed value.         3. Displays how many iterations were needed to produce         the result. */ #include using namespace std; int main() {   long seed, seed2, seed3, pnum, dnum, rem, counter, counter2;   bool prime, prime2;   // Let the user test multiple values   for (seed3 = -1; seed3 != 0; )   {   counter = counter2 = 1;   if(seed3 >= 0)         seed = seed3;   else   {cout << "Enter a number and I will tell\n"        << "you it's nearest prime number.\n";   cin >> seed;}   // Prevent the user from entering a negative number   while (seed <= 0)   {      cout << "Please enter a valid number (greater than 0)!\n";      cin >> seed ;   }   // Automatically says user's value is prime if they enter 1   if (seed == 1)        cout << seed << " is the smallest prime number!\n"             << "2 is the nearest larger prime number!\n";   else if (seed == 2)        cout << seed << " is a prime number!\n";   else   {   seed2 = seed ; // Defines the seed number for the smaller primes // Finds the nearest lager prime         for (prime = 0 ; prime != 1 ; seed++)         {   // Tests whether number is a prime                 dnum = 2          ;                 rem = seed % dnum ;                 if (rem == 0)                          prime = 0;                 else                 {                   for (pnum = seed, dnum = 3, prime = 1 ; dnum <= (pnum  / 2) && prime != 0; dnum += 2)                     {                         rem = pnum % dnum ;                         if (rem == 0)                             prime = 0;                         counter++;                     }                 }         }   // Finds the nearest smaller prime         for (prime2 = 0 ; prime2 != 1 ; seed2--)         {   // Tests whether number is a prime                 dnum = 2          ;                 rem = seed2 % dnum ;                 if (rem == 0)                    prime2 = 0;                 else                 {                    for (pnum = seed2, dnum = 3, prime2 = 1 ; dnum <= (pnum / 2) && prime2 != 0 ; dnum += 2)                     {                         rem = pnum % dnum ;                         if (rem == 0)                            prime2 = 0;                         counter2++ ;                     }                 }         }   cout << --seed << " is the nearest larger prime number!\n";   cout << ++seed2 << " is the nearest smaller prime number!\n";   cout << counter + counter2 << " calculations";   }   cout << "\nEnter another number (type '0' to quit)\n" ;   cin >> seed3;   cout << endl;   }    return 0; }

 Answer by kiran vellanki Submitted on 3/27/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great int main() { int a,b,c,d=0; printf("Enter the number : "); scanf("%d",&a); c=a/2; for(b=2;b<=c;b++) { if  (a%b==0) ++d; } if (d==0 && a>0) printf("\n\nThe number is prime"); else printf("\n\nThe number is not prime"); }

 Answer by vinaychimalgi Submitted on 4/17/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great This will give u answer in minimum clock cycles......i'e at maximum speed... #include #include voice main() { int i=0,n,mod,count=0; clrscr(); printf("/n Enter the number:- "); scanf("%d",&n); mod=n/2; for(i=1;i<=mod;i++) { if(n%i==0) count++; }; if(count==1) printf(" Number %d is prime ",n); else printf(" Number %d is not prime ",n); getch(); }

 Answer by Sathish.Ponnusamy Submitted on 4/25/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include main() {        int n,i=1,j,c;        clrscr();        printf("Enter Number Of Terms ");        printf("Prime Numbers Are Follwing ");        scanf("%d",&n);        while(i<=n)        {           c=0;           for(j=1;j<=i;j++)           {               if(i%j==0)               c++;           }            if(c==2)            printf("%d   ",i)            i++;         }         getch(); }

 Answer by Waleed Submitted on 6/24/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great Not really.. if(a=0) if (c=2) will always be true... CORRECTION:: if (a==0) if (c==0) After doing the correction.. the program seems to work..

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.