answersLogoWhite

0

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

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
BeauBeau
You're doing better than you think!
Chat with Beau
More answers

#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++;

}

}

User Avatar

Wiki User

12y ago
User Avatar

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 */ }

User Avatar

Wiki User

15y ago
User Avatar

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

}

}

User Avatar

Wiki User

8y ago
User Avatar

nee babu

User Avatar

Wiki User

13y ago
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?

To write a C program to find prime numbers between 1 to 500, you can use a nested loop structure. In the outer loop, iterate from 2 to 500, and in the inner loop, check if the number is divisible by any number from 2 to the square root of the number. If it is not divisible by any number other than 1 and itself, then it is a prime number. Print out all prime numbers found within the specified range. Remember to include necessary header files, such as &lt;stdio.h&gt;, and use appropriate logic to implement the program efficiently.


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