answersLogoWhite

0

C plus plus program to find prime number?

Updated: 12/8/2022
User Avatar

Jaiverma

Lvl 1
14y ago

Best Answer

Here's an example. I've included analysis.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main(void)

{

/* User inputs high and low ends of range to search.

/ This could easily be done as function inputs, if

/ if so desired. */

int testprime,testdivisor, minprime, maxprime;

printf("Search for primes between:\nLow end: ");

scanf("%d",minprime);

printf("Top end: ");

scanf("%d",maxprime);

int isprime; // 0 indicates a number is not prime..

for(testprime=minprime;testprime<=maxprime;testprime++)

{

isprime = 1;

for(testdivisor=2;testdivisor<sqrt(testprime);testdivisor++) // Primes divide by 1!

{

isprime = testprime%testdivisor; // % finds remainders, so 7%3 returns 1.

// If 0 is returned, a divisor has been found.

if(isprime == 0) break; // Hence, not prime. "break;" exits the loop.

}

if(isprime != 0) printf("%d is prime.\n",testprime);

}

system("pause");

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

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

What are prime number and composite numbers?

a prime number is a number that you can plus by one


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 prime number plus what prime number will give you 54?

47 + 7


Find N 3 plus 8 prime number?

3n+8=a To find the value of n, you need to know the value of a.


What is prime plus prime?

A prime plus another prime is at the very least going to be an even number since all primes (with the exception of 2) are odd.


Does x squared plus x plus 41 always equal a prime number?

No. it's not a prime number when x = 40, 41, 81,82 ...


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


Is number 5x3x17 plus 17 a prime number?

No. (5x3x17) + 17 =255 + 17 =272 (This is not prime.)


What prime number plus another prime number that is 28 numbers apart will give you 54?

The numbers are 13 and 41. And you do not need to know that they are prime in order to find the answer. The sum (54) and difference (28) provide all the necessary information.


Is 2187 plus 36295 a prime number?

No. The number 38,482 is composite.


What is the smallest number for n for which n2 plus n plus 17 is not a prime number?

n = 5.


C plus plus program to print number patterns?

bghjg