Program to Find a Prime Number

19
8/8/2019 Program to Find a Prime Number http://slidepdf.com/reader/full/program-to-find-a-prime-number 1/19 Internet RFC Index Usenet FAQ Index Other FAQs Documents Tools Search Search FAQs Search RFCs Cities Countries Hospitals Web Hosting Ratings [ Home | FAQ-Related Q&As | General Q&As | Answered Questions ] Search the Q&A Archives Search Q&As write a program to find out whether a given number is prime... << Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List) Question by urvi Submitted on 1/1/2004 Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List) Rating: Rate this question: write a program to find out whether a given number is prime or not. þÿ þÿ

Transcript of Program to Find a Prime Number

Page 1: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 1/19

• Internet RFC Index• Usenet FAQ Index• Other FAQs• Documents• Tools•

Search• Search FAQs• Search RFCs

• Cities• Countries• Hospitals•

Web Hosting Ratings

[ Home | FAQ-Related Q&As | General Q&As | Answered Questions ]

Search the Q&A Archives

Search Q&As

write a program to find out whether agiven number is prime...

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

Question by urvi

Submitted on 1/1/2004

Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

Rating: Rate this question:

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

þÿ

þÿ

Page 2: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 2/19

Answer by golla.satyanarayana

Submitted on 3/17/2004Rating: Rate this answer:

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:

#include<stdio.h>#include<conio.h>void main()

{int no,i;clrscr();printf(" Enter the Number U want to check:");scanf("%d",&no);i=2;while(i<=no-1){

þÿ

þÿ

Page 3: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 3/19

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:

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

þÿ

Page 4: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 4/19

Submitted on 5/19/2005

Rating: Not yet rated Rate this answer:

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:

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 ");

}

þÿ

þÿ

Page 5: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 5/19

Answer by romeo

Submitted on 2/20/2006

Rating: Not yet rated Rate this answer:

#include<stdio.h>#include<conio.h>#define TRUE 1#define FALSE 0

int prime(int number){

int i=2,flg=TRUE,c;while(i<number){

c = number %i;

if(c==0)flg=FALSE;

i++;}return flg;

}

void main(){

int num,c;int is_prime;

clrscr();printf("Enter a number: ");scanf("%d",&num);

is_prime = prime(num);

if(is_prime==1)puts("Prime Number");

elseputs("Not a Prime Number");

getch();

}

þÿ

Page 6: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 6/19

Answer by supriya

Submitted on 3/29/2006

Rating: Not yet rated Rate this answer:

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"); }

elseprintf("the given number is not prime");

}

Answer by raihana66

Submitted on 5/16/2006

Rating: Not yet rated Rate this answer:

int n,n1;print("enter number");scanf("%i",&n);if(n%2=0&&n1=2)printf("prime number");elseprintf("not primenumber);

Answer by raihana66

Submitted on 5/16/2006

þÿ

þÿ

Page 7: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 7/19

Rating: Not yet rated Rate this answer:

int n,n1;printf("enter number");scanf("%i",&n);if(n%2=1&&n1=2)printf("prime number");elseprintf("not prime number);

Answer by raihana66

Submitted on 5/16/2006

Rating: Not yet rated Rate this answer:

int n,c);printf("enter number");scanf("%i",&n);if(n%2=0&&n>=1&&c=2)printf("prime number");elseprintf("not prime number);

Answer by raihana

Submitted on 5/16/2006

Rating: Not yet rated Rate this answer:

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;

}

þÿ

þÿ

þÿ

Page 8: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 8/19

}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:

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");elseprintf(" NOT PRIME NUMBER");

getch();}

Answer by priya

Submitted on 7/23/2006

þÿ

Page 9: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 9/19

Rating: Not yet rated Rate this answer:

4 IS NOT A PRIME

Answer by Sudhakar Desaboina

Submitted on 8/23/2006

Rating: Not yet rated Rate this answer:

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");

elseprintf("the given number is not prime");}

Answer by Santosh

Submitted on 8/31/2006

Rating: Not yet ratedRate this answer:

// program for finding prime number

#include<stdio.h>

int isPrime(int n){

þÿ

þÿ

þÿ

Page 10: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 10/19

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:

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;

þÿ

Page 11: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 11/19

if(a==0){

c=c+1;}

}

if (c==2){ printf("the given number is prime"); }

elseprintf("the given number is not prime");

}

Answer by neeraj

Submitted on 11/11/2006

Rating: Not yet rated Rate this answer:

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"); }

elseprintf("the given number is not prime");

}

þÿ

Page 12: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 12/19

Answer by sundar

Submitted on 12/29/2006

Rating: Not yet rated Rate this answer:

#include <stdio.h>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:

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)

þÿ

þÿ

Page 13: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 13/19

{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:

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;}elseprintf("It is a prime number\n");}}}

Answer by fearfact_53

Submitted on 2/9/2007

Rating: Not yet rated Rate this answer:

þÿ

þÿ

Page 14: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 14/19

This program runs, if you enter a add number that is not prime it will tell youthat it is a prime number. Any way nice try. Email me if you want 1 thatworks. ( [email protected])

Answer by KDW

Submitted on 2/19/2007

Rating: Not yet rated Rate this answer:

// 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 theprogram moves on to the next incremented value aboveor below the seed.2. When testing whether a particular seed is a primethe program only test up to 1/2 of the seed value.3. Displays how many iterations were needed to producethe result.

*/

#include <iostream>using namespace std;

int main(){

long seed, seed2, seed3, pnum, dnum, rem, counter, counter2;bool prime, prime2;

// Let the user test multiple valuesfor (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

þÿ

Page 15: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 15/19

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 1if (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 primednum = 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 primednum = 2 ;rem = seed2 % dnum ;if (rem == 0)

prime2 = 0;else{

for (pnum = seed2, dnum = 3, prime2 = 1 ; dnum <= (pnum / 2) &&

Page 16: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 16/19

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:

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");elseprintf("\n\nThe number is not prime");

þÿ

Page 17: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 17/19

Page 18: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 18/19

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:

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

þÿ

Page 19: Program to Find a Prime Number

8/8/2019 Program to Find a Prime Number

http://slidepdf.com/reader/full/program-to-find-a-prime-number 19/19

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

below.

Your name or nickname:

If you'd like to create a newaccount or access your existingaccount, put in your passwordhere:

Your answer:

Check spelling

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 damagesresulting from publishing your answer.

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personalopinion 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 opinionor any product or service mentioned mentioned in these posts.

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

[ Home | FAQ-Related Q&As | General Q&As | Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.Contact Us Terms of Use

þÿ

þÿ