answersLogoWhite

0


Best Answer

#include<stdio.h>

bool is_prime (unsigned n) {

if (n<2) return false;

if (!(n%2)) return n==2;

unsigned max_factor = (unsigned) sqrt (n) + 1;

unsigned factor;

for (factor=3; factor<max_factor; ++factor) {

if (!(n%factor)) return false;

}

return true;

}

unsigned next_prime (unsigned n) {

while (!is_prime (++n));

return n;

}

int main() {

/* print the first 100 prime numbers */

unsigned i, n=0;

for (i=0; i<100; ++i) {

n = next_prime (n);

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

}

return 0;

}

User Avatar

Wiki User

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

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

void main()

{

int no, i;

clrscr();

printf("\nEnter a number : \n");

scanf("%d", &no);

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

if(no%i==0)

break;

if(i==no)

printf("\nEntered Number %d is Prime", no);

else

printf("\nEntered Number %d is not a Prime number", no);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Use the following functions to test if a value is prime or not (to test negative values, use the absolute value):

// helper functions (test the state of LSB to determine if value is odd or even)

bool is_even (const unsigned value) {return (value&0x1)==0x0; }

bool is_odd (const unsigned value) {return (value&0x1)==0x1; }

bool is_prime (const unsigned value)

{

if (value<2) return false; // 0 and 1 are non-prime

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

// Test all odd numbers from 3 to the square root of value, see if any are factors

unsigned number;

const unsigned max;

max=sqrt (value);

for (number=3; number<=max; number+=2)

{

if ((value%number)==0) return false; // number is a factor, value is non-prime

}

return true; // value is prime -- it has only 2 factors, 1 and itself

};

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Any number that has one or more prime factors is a composite number. All others are prime numbers. Prime factors are themselves prime numbers, but rather than test every potential factor to determine if it is prime, it is much quicker to simply test every odd factor in the range 3 to the square root of the number. The only exception is even numbers which are all composite except 2.

Thus we use the following algorithm (for all positive integers):

1) If the number is less than 2 then the number is composite.

2) If the number is even then it is prime if it is 2 otherwise it is composite.

3) If the number is odd and is evenly divisible by any odd integer in the range 3 to the square root of the number (inclusive) then the number is composite.

4) All other numbers are prime.

To express this algorithm as a function in C++, we would use the following:

bool is_prime (unsigned value)

{

// algorithm part 1)

if (value<2)

return false;

// algorithm part 2)

if (!(value%2))

return value==2;

// algorithm part 3)

unsigned max_factor = (unsigned) sqrt (value) + 1;

for (unsigned factor=3; factor<max_factor; ++factor)

{

if (!(value%factor))

return false;

}

// algorithm part 4)

return true;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Basically, you have a number of which you want to figure out whether it is a prime number or not. If you are not given such a number, try out whether different numbers are prime numbers, until you find one that is a prime number.

A simple method is to divide your number by all possible factors, starting at 2, and up to the number itself. If the smallest factor is the number itself, then it is a prime number. If a smaller factor is found, then it is not. Here is one way you can do that:

boolean isPrime(int number) {

int factor;

for (factor = 2; number % factor != 0; factor++)

;

return number == factor;

}

Note that the "for" loop will continue running until a factor is found.

Also note that this is not the most efficient method; just one that is fairly easy to program.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

You will need two functions for this, one to determine if a given number is prime or not, and another to find the next prime from a given number.

bool is_prime (const unsigned int num) {

if (num<2) return false;

if (!(num%2)) return num==2;

const unsigned int max_factor = sqrt (num) + 1;

for (unsigned int factor=3; factor<max_factor; factor+=2)

if (!(num%factor)) return false;

return true;

}

unsigned int next_prime (unsigned int num) {

while (!is_prime(++num));

return num;

}

int main (void) {

unsigned int num = 2;

do {

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

num = next_prime (num);

} while (num<=100);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

You should use modulus division, which gives you the remainder. In C++ modulus division uses the % sign.

For example:

5 % 2 = 1

4 % 2 = 0

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a java program to find a prime number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a program in lisp to find if a number is prime or not?

(defun prime (num) (if (&lt; 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (&lt;= chk-to dividend) (return t))) t))


How do you write a program in Python to find the first n prime numbers?

One way to do this is to write a function that checks if a number is prime: def isPrime(number): for i in range(2, number): if number%i == 0: return False return True Then create variables to track how many primes have been found and a variable to track which number is being tested for being prime. Increment that variable and test it, and if it is prime print it out (or save it somewhere) and increment the variable being used to track how many primes have been found: targetPrimes = 10 #number of primes to find primesFound = 0 #number of primes found i = 1 while (primesFound &lt; targetPrimes): i += 1 #first number tested is 2 if isPrime(i): print(i) primesFound += 1


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

Here is a simple program to generate prime numbers upto a given specific number /*Prime No. from 1 to 50*/ /*By-Himanshu Rathee*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int i,j,n; clrscr(); printf(" Enter the number upto which we have to find the prime number: "); scanf("%d",&amp;n); printf("\n"); for(i=2;i&lt;=n;i++) { for(j=2;j&lt;=i-1;j++) if(i%j==0) break; /*Number is divisble by some other number. So break out*/ if(i==j) printf("\t%d",i); /*Number was divisible by itself (that is, i was same as j)*/ } /*Continue loop upto nth number*/ getch(); }


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


How do you write Square program using vb?

write a vb program to find the magic square

Related questions

What is the largest prime no that is stored in 8 bit pattern?

Write your own prime number program and find out.


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 VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


With a given big integer number which is the product of two prime numbers how do you find them?

First write a program to generate the prime number. After one prime number was generated, divide the big int number by the prime number. If the remainder is zero then quotient is the second prime number ( also it is important to check whether the quotient is prime number or not because sometimes you will get wrong answer). Repeat the process until you get the result.


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.


write a pseudocode to accept a number and find if it is prime or not?

Yes


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.


Prime numbers between 1 to 10 in microprocessor 8085?

program to find prime number in 8085 microprocessor


Write a Shell program to find the smallest number from a set of numbers?

k


Write a java programme to check whether the number is twin prime or not?

Find a prime number, add 2 to the number. Check if the new number is prime. IE : 3 is prime. 3+2 =5. 5 is prime. (3,5) are twin primes.


How do you write a program to find whether the given number is prime or not and show output?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int n,a=2; clrscr(); printf("\n enter n"); scanf("%d",&amp;n); if(i&lt;=a-1) { if(a%i==0) { printf("\n the given number is not a prime number"); break; } i++; if(a==i) { printf("\n the given number is a prime number"); } getch(); output: enter the value of n:2 the given number is prime number


How do you write a program in lisp to find if a number is prime or not?

(defun prime (num) (if (&lt; 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (&lt;= chk-to dividend) (return t))) t))