What's the function for a program to find a prime number? I basically want to make something that tells me if a number entered is a prime number.
I know I'll need a cmath function i think?
a cin for num1
a cout for a true of false value
I can't figure out how to write the single line of code which is for a prime number though... If I had that, it's all smooth sailing lol. I know the definition of a prime is a number that can only be divided by one and itself...
so maybe like...
if (num1/1) = num1 <--- divide by one
not sure about divided by itself?
why don't u USE GOOGLE??
it took me 5 seconds to find out what prime number mean:)
are the numbers than just can be divided between themselves and one (1).
example:
2,3,5,7,11,13,17,19,23
4 is not a prime number because is the result of 2*2
6 is not a prime number bacause is the result of 3*2
8 is not a prime number bacause is the result of 2*2*2
9 is not a prime number bacause is the result of 3*3
10 is not a prime number bacause is the result of 5*2
12 is not a prime number bacause is the result of 2*3*2
14 is not a prime number bacause is the result of 7*2
now when we know what prime number is we can make some code :D
#include <iostream>
usingnamespace std;
void prime_num(int);
int main() {
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
return 0;
}
void prime_num( int num){
bool isPrime=true;
for ( int i = 0; i <= num; i++) {
for ( int j = 2; j <= num; j++) {
if ( i!=j && i % j == 0 ) {
isPrime=false;
break;
}
}
if (isPrime)
cout <<"Prime:"<< i << endl;
isPrime=true;
}
}
The % operator tells you the remainder of the division. If you find a number (below the number in question) that has no remainder then it's not a prime number.
um. i'm curious to know something... using the code above, i want to eliminate the cin values and only come up with a return value of prime numbers say... from 1 to 20.
so i know i'll have to put initial as 1 and then a stop at 20... how would i go about doing that?