answersLogoWhite

0

C program to find prime number?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

I am providing a succinct and easy to understand version of the program. I have run it in 3-4 compilers and it works perfect. Mind you, you should not enter a number more than 2147483647 (which is the largest number a variable can process in C!). If you do, no problem, but it will display all numbers above it, including the even numbers to be prime. So here you are:

#include

#include

main()

{

long int a,b,c;

printf("Enter the number: ");

scanf("%ld",&a);

for (b=2;b

{

c= a%b;

if (c==0)

goto cool;

}

printf("\nThe number is goddamned prime!");

goto fool;

cool: printf("\nThis ain't a Prime number you dumbo!");

fool: printf("\n");

}

By the way I am not sure what your query really was. I gave the above program to check whether a number is prime or not. If you mean to generate prime numbers between 2 (which is of course the first prime number) and a given number g and also want to know the number of prime numbers between the two, well here is it:

#include

#include

main()

{

long int a=2,i=2,c,count=0,g;

printf("Prime numbers upto what number should be generated? ");

scanf("%ld",&g);

printf("\nFollowing is the list of prime numbers:\n");

while (a<=g)

{

for (i=2;i

{

c=(a%i);

if (c==0)

goto cool;

else

continue;

}

printf("\n%ld",a);

++count;

cool: ++a;

}

printf("\nTotal number of prime numbers between 2 and %ld is %ld. ",g,count);

}

For both these programs, simply copy-paste them into your compiler and have your result. Hope you got what you wanted...

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include<stdio.h>

#include<conio.h>

main()

{

int i,j,n;

printf(" Enter the number upto which you want to find prime number ");

scanf("%d",&n);

printf("\n");

for(i=2;i<=n;i++)

{

for(j=2;j<=i-1;j++)

if(i%j==0)

break;

if(i==j) printf("\n\t%d",i);

}

getch();

}

//for example if u want to find prime number between 1 to 100 enter 100..

// u will get prime number between 1 to 100

//tested in bloodshed dev c++

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

clrscr();

printf("\nenter a number...\n");

scanf("%d",&n);

if (n!=2 && n%2==0)

printf("%d isn't prime..", n);

else

printf("%d is either prime or not..", n);

getch();

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program to find prime number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you find prime no in gwbasic program?

10 input "enter a no."; n 20 for i = 1 to n 30 if n mod i = 0 then c = c +1 40 next i 50 if c = 2 then print "prime number" else print "not a prime number" 60 end


Write a c program to find given number is prime or not?

Yes, do write, or if you're too lazy to your homework, use google.


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


How do you write a c program to get a range from user and give a list of prime numbers?

To get all tutorials of "c programming" Reference:cprogramming-bd.com/c_page2.aspx# prime number


Write a C program to find the sum of all prime numbers?

Since there is an infinite set of prime numbers the answer would be infinity.


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


What is the Program for composite and prime numbers in c?

#include&lt;iostream.h&gt; #include&lt;conio.h&gt; int main() { int i,n; clrscr(); cout&lt;&lt;"PROGRAM TO CHECK IF THE NUMBER IS PRIME OR NOT:"&lt;&lt;endl; cout&lt;&lt;"Enter a number:"; cin&gt;&gt;n; for(int i=2;i&lt;n;i++) { if(n%i==0) cout&lt;&lt;"\nTHE NUMBER IS COMPOSITE"&lt;&lt;endl; else cout&lt;&lt;"\nTHE NUMBER IS PRIME"&lt;&lt;endl; } return 0; }


Program for sin series in c language?

find the program in c-pgms.blogspot.com


Writes a c program to find the sum of all integers between 1 and n?

Write a program to find the number and sum of all integers from 100 to 300 that are divisible by 11


Program in c language to find prime factors of a given number?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int n,i; clrscr(); printf("Enter a Number:"); scanf("%d",&amp;n); printf("\n\nPrime Factors of %d is: ",n); for(i=2;i&lt;=n;i++) { if(n%i==0) { printf("%d,",i); n=n/i; i--; if(n==1) break; } } getche(); } this program will find the prime factors of a given numbe any assistance mail at :- devilllcreature@yahoo.com thank you


How can you check whether a number is prime or not by a C program using functions?

#include&lt;iostream.h&gt; #include&lt;conio.h&gt; void prime(int n) { clrscr(); int num; cout&lt;&lt;"enter the numbers"&lt;&lt;endl; cin&gt;&gt;num; prime(num); getch(); } void prime(int n) { int prime=1,i; for(i=2;i&lt;=n/2;i++) if(n%i==1) prime=0; if(prime==1) cout&lt;&lt;"the number"&lt;&lt;n&gt;&gt;"is prime"; else cout&lt;&lt;"the number"&lt;&lt;n&lt;&lt;"is not prime"; }


How do you write socket program in c?

For first find an example program.