answersLogoWhite

0


Best Answer

A number is a Prime number if it is divisible by only 1 and itself. So make use of for loop and check for this condition.

int i,num,flag = 0;//num is the number to check

scanf("%d", &num);

for( i=2; i

if( num%i == 0)//it has other divisors

flag = 1;

}

if( !flag)

printf("The number is prime\n");

else

printf("The number is not prime\n");


User Avatar

Wiki User

βˆ™ 11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 12y ago

#include<stdio.h>

void main()

{

int n = 0, i = 3, count = 0, loop = 0;

printf("Enter the number of prime numbers required\n");

scanf("%d",&n);

if ( n >= 1 )

{

printf("First %d prime numbers are :\n",n);

printf("2\n");

}

for ( count = 2 ; count <= n ; )

{

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

{

if ( i%loop i )

{

printf("%d\n",i);

count++;

}

i++;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

int isprime(int n) { int t; if ((int) (n/2) * 2 == n) return 0; /* number is even - return false */ for (t=3; t*t<n; t+=2) if ((int) (n/t) * t == n) return 0; /* divisible - return false */ return 1; /* prime - return true */ }

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 8y ago

#include<iostream> #include<cmath>

bool is_odd (const unsigned num) {

return num & 1 == 1;

}

bool is_even (const unsigned num) {

return is_odd (num) == false;

}

bool is_prime (const unsigned num) {

if (num<2) return false;

if (is_even (num) == true) return num==2; // 2 is the only even prime

const unsigned max = (unsigned) sqrt (num) + 1;

unsigned div;

for (div=3; div<max; div+=2) {

if (num%div == 0) return false;

}

return true;

}

int main (void) {

std::cout << "Enter a whole number: ";

std::cin >> num;

if (is_even (abs (num)) {

std::cout << "The number is even\n";

}else{

std::cout << "The number is odd\n";

}

if (is_prime (abs (num)) {

std::cout << "The number is prime\n";

}else{

std::cout << "The number is composite\n";

}

}

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

nee babu

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for prime number
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the C programming of check the prime number with flow chart?

flow t prime numberchar


C program to find prime number?

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#includemain(){long int a,b,c;printf("Enter the number: ");scanf("%ld",&a);for (b=2;b


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a c program to find out the prime numbers between 1 to 500?

Oh, dude, you wanna find prime numbers? Alright, here's a simple C program for ya: You loop through numbers from 1 to 500, check if each number is prime by dividing it only by numbers up to its square root (efficient, right?), and if it's only divisible by 1 and itself, boom, you got a prime number. Print those bad boys out and voilΓ , you're done. Like, easy peasy, lemon squeezy!


C plus plus code that determine prime number?

#include &lt;iostream.h&gt; main() { int a; cout&lt;&lt;"enter a number : "; cin&gt;&gt;a; cout&lt;&lt;endl; if (a%2-1) cout&lt;&lt;"it is a prime number"; else cout&lt;&lt;"it is not a prime number" return 0; } ------------------------------------------ output: enter a number : 30 it is a not a prime number

Related questions

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


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.


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


A program to find that the input number is prime or not?

Use Wolfram|Alpha... go to the related link below, Wolfram|Alpha, and type in (is __ (number) prime) and then the program will compute that and tell you if it is prime or composite.


Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


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 a program in objective c numbers 1-100 prime numbers?

fdsgfhgdfhgdf


Which number is a prime number a.14 b.15 c.23 d.25?

c.23


When you multiply a prime and a composite number do you always get a composite number?

Product of a prime number and a composite number results in a composite number.Now consider the product of a composite number(a) and a prime number(b) is equal to c.i.e. c = a x bIt is clear that c is divisible by both a and b.Also c is divisible by itself and 1, this means that c has more than two factors.A number having more than two factors is composite, therefore product of a prime number and a composite number results in a composite number.