answersLogoWhite

0

C program for prime number

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

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

11y 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

14y 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

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


Is C a prime or composite or square number?

The answer depends on the value of C, which is unknown.