Im doing exercise from the Bjarne Stroustrup book called "Programming principles and practice using c++". I have to find first n prime numbers that user wants[user enters 5, and program finds first five prime numbers]. I found the solution on this site:
http://people.ds.cam.ac.uk/nmm1/C++/Exercises/Chapter_04/ex_15.cpp
bool prime (vector<int> table, int number) {
for (int i = 0; i < table.size(); ++i)
if (number%table[i] == 0) return false;
return true;
}
but i cant understand the test for primality. Why modulo? I have my own test for primality and its much easier to understand for me, albeit its less elegant and more verbose.
bool isPrime(int num) {
for (int i = 2; i < num; i++) {
for (int j = 0; j < num; j++) {
if (i*j == num) {
return false;
}
}
}
if (num == 1) {
return false;
}
return true;
}
So if anyone can explain me that guy code i would be greatful.
Here's the other code, included in its entirety:
#include "std_lib_facilities.h"
bool prime (vector<int> table, int number) {
for (int i = 0; i < table.size(); ++i)
if (number%table[i] == 0) return false;
return true;
}
int main () {
int count, next;
cout << "Input the number of primes\n";
cin >> count;
vector<int> table;
next = 2;
while (table.size() < count) {
if (prime(table,next)) table.push_back(next);
++next;
}
for (int n = 0; n < table.size(); ++n)
cout << table[n] << " ";
cout << endl;
// keep_window_open();
return 0;
}
This code populates the vector table with all the prime numbers it has found so far. Starting from 2, it checks if the number is divisible by any of the numbers in this table it has constructed. If it isn't divisible, it means that this number is a prime, and it gets entered into the table.
The modulo operator is used to check for divisibility here. a%b returns the remainder that occurs when the division a/b is performed. If this value is 0, there is no remainder, and we can conclude that a is divisible by b.
The modulo operator returns the remainder of a division operation. So if the remainder is equal to 0 for any of the numbers in the table, then we can say that the number is evenly divisible, and therefore not prime. On the other hand, if the modulo returns anything other than 0 for all of the numbers in the table, the number is not evenly divisible, and therefore it is prime.
https://www.cprogramming.com/tutorial/modulus.html
Welcome to StackOverflow :)
I am not sure whether I should ask here or programmers but I have been trying to work out why this program wont work and although I have found some bugs, it still returns "x is not a prime number", even when it is.
#include <iostream>
using namespace std;
bool primetest(int a) {
int i;
//Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
int b = a / 2;
//Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
for (i = 2; i < b; i++) {
//If the user input has no remainder then it cannot be a prime and the loop can stop (break)
if (a % i == 0) {
return(0);
break;
}
//Other wise if the user input does have a remainder and is the last of the loop, return true (it is a prime)
else if ((a % i != 0) && (i == a -1)) {
return (1);
break;
}
}
}
int main(void) {
int user;
cout << "Enter a number to test if it is a prime or not: ";
cin >> user;
if (primetest(user)) {
cout << user << " is a prime number.";
}
else {
cout << user<< " is not a prime number.";
}
cout << "\n\nPress enter to exit...";
getchar();
getchar();
return 0;
}
Sorry if this is too localised (in which case could you suggest where I should ask such specific questions?)
I should add that I am VERY new to C++ (and programming in general)
This was simply intended to be a test of functions and controls.
i can never be equal to a - 1 - you're only going up to b - 1. b being a/2, that's never going to cause a match.
That means your loop ending condition that would return 1 is never true.
In the case of a prime number, you run off the end of the loop. That causes undefined behaviour, since you don't have a return statement there. Clang gave a warning, without any special flags:
example.cpp:22:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
1 warning generated.
If your compiler didn't warn you, you need to turn on some more warning flags. For example, adding -Wall gives a warning when using GCC:
example.cpp: In function ‘bool primetest(int)’:
example.cpp:22: warning: control reaches end of non-void function
Overall, your prime-checking loop is much more complicated than it needs to be. Assuming you only care about values of a greater than or equal to 2:
bool primetest(int a)
{
int b = sqrt(a); // only need to test up to the square root of the input
for (int i = 2; i <= b; i++)
{
if (a % i == 0)
return false;
}
// if the loop completed, a is prime
return true;
}
If you want to handle all int values, you can just add an if (a < 2) return false; at the beginning.
Your logic is incorrect. You are using this expression (i == a -1)) which can never be true as Carl said.
For example:-
If a = 11
b = a/2 = 5 (Fractional part truncated)
So you are running loop till i<5. So i can never be equal to a-1 as max value of i in this case will be 4 and value of a-1 will be 10
You can do this by just checking till square root. But below is some modification to your code to make it work.
#include <iostream>
using namespace std;
bool primetest(int a) {
int i;
//Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
int b = a / 2;
//Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
for (i = 2; i <= b; i++) {
//If the user input has no remainder then it cannot be a prime and the loop can stop (break)
if (a % i == 0) {
return(0);
}
}
//this return invokes only when it doesn't has factor
return 1;
}
int main(void) {
int user;
cout << "Enter a number to test if it is a prime or not: ";
cin >> user;
if (primetest(user)) {
cout << user << " is a prime number.";
}
else {
cout << user<< " is not a prime number.";
}
return 0;
}
check this out:
//Prime Numbers generation in C++
//Using for loops and conditional structures
#include <iostream>
using namespace std;
int main()
{
int a = 2; //start from 2
long long int b = 1000; //ends at 1000
for (int i = a; i <= b; i++)
{
for (int j = 2; j <= i; j++)
{
if (!(i%j)&&(i!=j)) //Condition for not prime
{
break;
}
if (j==i) //condition for Prime Numbers
{
cout << i << endl;
}
}
}
}
main()
{
int i,j,x,box;
for (i=10;i<=99;i++)
{
box=0;
x=i/2;
for (j=2;j<=x;j++)
if (i%j==0) box++;
if (box==0) cout<<i<<" is a prime number";
else cout<<i<<" is a composite number";
cout<<"\n";
getch();
}
}
Here is the complete solution for the Finding Prime numbers till any user entered number.
#include <iostream.h>
#include <conio.h>
using namespace std;
main()
{
int num, i, countFactors;
int a;
cout << "Enter number " << endl;
cin >> a;
for (num = 1; num <= a; num++)
{
countFactors = 0;
for (i = 2; i <= num; i++)
{
//if a factor exists from 2 up to the number, count Factors
if (num % i == 0)
{
countFactors++;
}
}
//a prime number has only itself as a factor
if (countFactors == 1)
{
cout << num << ", ";
}
}
getch();
}
One way is to use a Sieving algorithm, such as the sieve of Eratosthenes. This is a very fast method that works exceptionally well.
bool isPrime(int number){
if(number == 2 || number == 3 | number == 5 || number == 7) return true;
return ((number % 2) && (number % 3) && (number % 5) && (number % 7));
}
I'm trying to figure out in c++ how to find all the prime numbers in a range (using 100 for now)
I'm not to concerned about performance, I'm starting out in c++ and trying to understand this program exercise from my book. I have my program I'm trying to use below but it keeps returning false. Any ideas? I've read through almost all of googles/bing's help as well as stack overflow. I can write code for it to work with inputting the number; just not looping through all numbers
any ideas on what i'm doing wrong?
#include <iostream>
using namespace std;
bool isPrime(long n);
int main()
{
int i;
//some vars
char emptyVar;
//first loop (to increment the number)
for (i = 0; i <= 100; i++)
{
//checking all numbers below 100
if (isPrime(i) == true)
{
//is true
cout << i << ", ";
}
else if (isPrime(i) == false)
{
//is false
cout <<"false , ";
}
}
cin >> emptyVar;
}
bool isPrime(long n)
{
long i =0;
//checks to see if the number is a prime
for (i = 2; i < n; i++) // sqrt is the highest possible factor
{
if ( n % i == 0) // when dividing numbers there is no remainder if the numbers are both factors
{
// is a factor and not prime
return false;
}
else if (n % i != 0 && i >= 100)
{
//is not a factor
return true;
}
}
}
The function isPrime does not have a return statement for every possible path of execution. For example, what does isPrime do, when n == 2?
Here's how a for loop works (in pseudo code). The general syntax is
for (initialiazion; condition; increment) {
body;
}
rest;
This can be translated into a while-loop:
initialiazion;
while (condition) {
body;
increment;
}
rest;
Especially, the condition is checked right after the intialization, before body is executed.
I suspect, you think that a for loop works like this:
initialiazion;
do {
body;
increment;
} while (condition);
rest;
i.e. the condition is checked after the first increment. But it doesn't.
It should return true if it's not a factor of EVERY i, not just the first one it encounters.
bool isPrime(long n)
{
long i =0;
//checks to see if the number is a prime
for (i = 2; i < n ; i++) // sqrt is the highest possible factor
{
if ( n % i == 0) // when dividing numbers there is no remainder if the numbers are both factors
{
// is a factor and not prime
return false;
}
}
return true;
}
Also in your case you doesn't make sense to search beyond i > n/2.
Of course you should give a look to the literature, the are really robust primality test algorithms.
Your isPrime function is incorrect. It should check all numbers and only then return true;
And this block wouldn't be ever called on your inputs:
else if (n % i != 0 && i >= 100)
{
//is not a factor
return true;
}
I have perused a lot of code on this topic, but most of them produce the numbers that are prime all the way up to the input number. However, I need code which only checks whether the given input number is prime.
Here is what I was able to write, but it does not work:
void primenumber(int number)
{
if(number%2!=0)
cout<<"Number is prime:"<<endl;
else
cout<<"number is NOt prime"<<endl;
}
I would appreciate if someone could give me advice on how to make this work properly.
Update
I modified it to check on all the numbers in a for loop.
void primenumber(int number)
{
for(int i=1; i<number; i++)
{
if(number%i!=0)
cout<<"Number is prime:"<<endl;
else
cout<<"number is NOt prime"<<endl;
}
}
bool isPrime(int number){
if(number < 2) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(int i=3; (i*i)<=number; i+=2){
if(number % i == 0 ) return false;
}
return true;
}
My own IsPrime() function, written and based on the deterministic variant of the famous Rabin-Miller algorithm, combined with optimized step brute forcing, giving you one of the fastest prime testing functions out there.
__int64 power(int a, int n, int mod)
{
__int64 power=a,result=1;
while(n)
{
if(n&1)
result=(result*power)%mod;
power=(power*power)%mod;
n>>=1;
}
return result;
}
bool witness(int a, int n)
{
int t,u,i;
__int64 prev,curr;
u=n/2;
t=1;
while(!(u&1))
{
u/=2;
++t;
}
prev=power(a,u,n);
for(i=1;i<=t;++i)
{
curr=(prev*prev)%n;
if((curr==1)&&(prev!=1)&&(prev!=n-1))
return true;
prev=curr;
}
if(curr!=1)
return true;
return false;
}
inline bool IsPrime( int number )
{
if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
return (false);
if(number<1373653)
{
for( int k = 1; 36*k*k-12*k < number;++k)
if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
return (false);
return true;
}
if(number < 9080191)
{
if(witness(31,number)) return false;
if(witness(73,number)) return false;
return true;
}
if(witness(2,number)) return false;
if(witness(7,number)) return false;
if(witness(61,number)) return false;
return true;
/*WARNING: Algorithm deterministic only for numbers < 4,759,123,141 (unsigned int's max is 4294967296)
if n < 1,373,653, it is enough to test a = 2 and 3.
if n < 9,080,191, it is enough to test a = 31 and 73.
if n < 4,759,123,141, it is enough to test a = 2, 7, and 61.
if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11.
if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13.
if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.*/
}
To use, copy and paste the code into the top of your program. Call it, and it returns a BOOL value, either true or false.
if(IsPrime(number))
{
cout << "It's prime";
}
else
{
cout<<"It's composite";
}
If you get a problem compiling with "__int64", replace that with "long". It compiles fine under VS2008 and VS2010.
How it works:
There are three parts to the function. Part checks to see if it is one of the rare exceptions (negative numbers, 1), and intercepts the running of the program.
Part two starts if the number is smaller than 1373653, which is the theoretically number where the Rabin Miller algorithm will beat my optimized brute force function. Then comes two levels of Rabin Miller, designed to minimize the number of witnesses needed. As most numbers that you'll be testing are under 4 billion, the probabilistic Rabin-Miller algorithm can be made deterministic by checking witnesses 2, 7, and 61. If you need to go over the 4 billion cap, you will need a large number library, and apply a modulus or bit shift modification to the power() function.
If you insist on a brute force method, here is just my optimized brute force IsPrime() function:
inline bool IsPrime( int number )
{
if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
return (false);
for( int k = 1; 36*k*k-12*k < number;++k)
if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
return (false);
return true;
}
}
How this brute force piece works:
All prime numbers (except 2 and 3) can be expressed in the form 6k+1 or 6k-1, where k is a positive whole number. This code uses this fact, and tests all numbers in the form of 6k+1 or 6k-1 less than the square root of the number in question. This piece is integrated into my larger IsPrime() function (the function shown first).
If you need to find all the prime numbers below a number, find all the prime numbers below 1000, look into the Sieve of Eratosthenes. Another favorite of mine.
As an additional note, I would love to see anyone implement the Eliptical Curve Method algorithm, been wanting to see that implemented in C++ for a while now, I lost my implementation of it. Theoretically, it's even faster than the deterministic Rabin Miller algorithm I implemented, although I'm not sure if that's true for numbers under 4 billion.
You need to do some more checking. Right now, you are only checking if the number is divisible by 2. Do the same for 2, 3, 4, 5, 6, ... up to number. Hint: use a loop.
After you resolve this, try looking for optimizations.
Hint: You only have to check all numbers up to the square root of the number
I would guess taking sqrt and running foreach frpm 2 to sqrt+1 if(input% number!=0) return false;
once you reach sqrt+1 you can be sure its prime.
C++
bool isPrime(int number){
if (number != 2){
if (number < 2 || number % 2 == 0) {
return false;
}
for(int i=3; (i*i)<=number; i+=2){
if(number % i == 0 ){
return false;
}
}
}
return true;
}
Javascript
function isPrime(number)
{
if (number !== 2) {
if (number < 2 || number % 2 === 0) {
return false;
}
for (var i=3; (i*i)<=number; i+=2)
{
if (number % 2 === 0){
return false;
}
}
}
return true;
}
Python
def isPrime(number):
if (number != 2):
if (number < 2 or number % 2 == 0):
return False
i = 3
while (i*i) <= number:
if(number % i == 0 ):
return False;
i += 2
return True;
If you know the range of the inputs (which you do since your function takes an int), you can precompute a table of primes less than or equal to the square root of the max input (2^31-1 in this case), and then test for divisibility by each prime in the table less than or equal to the square root of the number given.
This code only checks if the number is divisible by two. For a number to be prime, it must not be evenly divisible by all integers less than itself. This can be naively implemented by checking if it is divisible by all integers less than floor(sqrt(n)) in a loop. If you are interested, there are a number of much faster algorithms in existence.
If you are lazy, and have a lot of RAM, create a sieve of Eratosthenes which is practically a giant array from which you kicked all numbers that are not prime.
From then on every prime "probability" test will be super quick.
The upper limit for this solution for fast results is the amount of you RAM. The upper limit for this solution for superslow results is your hard disk's capacity.
I follow same algorithm but different implementation that loop to sqrt(n) with step 2 only odd numbers because I check that if it is divisible by 2 or 2*k it is false. Here is my code
public class PrimeTest {
public static boolean isPrime(int i) {
if (i < 2) {
return false;
} else if (i % 2 == 0 && i != 2) {
return false;
} else {
for (int j = 3; j <= Math.sqrt(i); j = j + 2) {
if (i % j == 0) {
return false;
}
}
return true;
}
}
/**
* #param args
*/
public static void main(String[] args) {
for (int i = 1; i < 100; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
}
Use mathematics first find square root of number then start loop till the number ends which you get after square rooting.
check for each value whether the given number is divisible by the iterating value .if any value divides the given number then it is not a prime number otherwise prime.
Here is the code
bool is_Prime(int n)
{
int square_root = sqrt(n); // use math.h
int toggle = 1;
for(int i = 2; i <= square_root; i++)
{
if(n%i==0)
{
toggle = 0;
break;
}
}
if(toggle)
return true;
else
return false;
}
bool check_prime(int num) {
for (int i = num - 1; i > 1; i--) {
if ((num % i) == 0)
return false;
}
return true;
}
checks for any number if its a prime number
Someone had the following.
bool check_prime(int num) {
for (int i = num - 1; i > 1; i--) {
if ((num % i) == 0)
return false;
}
return true;
}
This mostly worked. I just tested it in Visual Studio 2017. It would say that anything less than 2 was also prime (so 1, 0, -1, etc.)
Here is a slight modification to correct this.
bool check_prime(int number)
{
if (number > 1)
{
for (int i = number - 1; i > 1; i--)
{
if ((number % i) == 0)
return false;
}
return true;
}
return false;
}
Count by 6 for better speed:
bool isPrime(int n)
{
if(n==1) return false;
if(n==2 || n==3) return true;
if(n%2==0 || n%3==0) return false;
for(int i=5; i*i<=n; i=i+6)
if(n%i==0 || n%(i+2)==0)
return false;
return true;
}
There are several different approches to this problem.
The "Naive" Method: Try all (odd) numbers up to (the root of) the number.
Improved "Naive" Method: Only try every 6n ± 1.
Probabilistic tests: Miller-Rabin, Solovay-Strasse, etc.
Which approach suits you depends and what you are doing with the prime.
You should atleast read up on Primality Testing.
If n is 2, it's prime.
If n is 1, it's not prime.
If n is even, it's not prime.
If n is odd, bigger than 2, we must check all odd numbers 3..sqrt(n)+1, if any of this numbers can divide n, n is not prime, else, n is prime.
For better performance i recommend sieve of eratosthenes.
Here is the code sample:
bool is_prime(int n)
{
if (n == 2) return true;
if (n == 1 || n % 2 == 0) return false;
for (int i = 3; i*i < n+1; i += 2) {
if (n % i == 0) return false;
}
return true;
}
I came up with this:
int counter = 0;
bool checkPrime(int x) {
for (int y = x; y > 0; y--){
if (x%y == 0) {
counter++;
}
}
if (counter == 2) {
counter = 0; //resets counter for next input
return true; //if its only divisible by two numbers (itself and one) its a prime
}
else counter = 0;
return false;
}
This is a quick efficient one:
bool isPrimeNumber(int n) {
int divider = 2;
while (n % divider != 0) {
divider++;
}
if (n == divider) {
return true;
}
else {
return false;
}
}
It will start finding a divisible number of n, starting by 2. As soon as it finds one, if that number is equal to n then it's prime, otherwise it's not.
//simple function to determine if a number is a prime number
//to state if it is a prime number
#include <iostream>
using namespace std;
int isPrime(int x); //functioned defined after int main()
int main() {
int y;
cout << "enter value" << endl;
cin >> y;
isPrime(y);
return 0;
} //end of main function
//-------------function
int isPrime(int x) {
int counter = 0;
cout << "factors of " << x << " are " << "\n\n"; //print factors of the number
for (int i = 0; i <= x; i++)
{
for (int j = 0; j <= x; j++)
{
if (i * j == x) //check if the number has multiples;
{
cout << i << " , "; //output provided for the reader to see the
// muliples
++counter; //counts the number of factors
}
}
}
cout << "\n\n";
if (counter > 2) {
cout << "value is not a prime number" << "\n\n";
}
if (counter <= 2) {
cout << "value is a prime number" << endl;
}
}
Here is a simple program to check whether a number is prime or not:
#include <iostream>
using namespace std;
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
return 0;
}
Here is a C++ code to determine that a given number is prime:
bool isPrime(int num)
{
if(num < 2) return false;
for(int i = 2; i <= sqrt(num); i++)
if(num % i == 0) return false;
return true;
}
PS Don't forget to include math.h library to use sqrt function
well crafted, share it with you:
bool isPrime(int num) {
if (num == 2) return true;
if (num < 2) return false;
if (num % 2 == 0) return false;
for (int i = num - 1; i > 1; i--) {
if (num % i == 0) return false;
}
return true;
}
There are many potential optimization in prime number testing.
Yet many answers here, not only are worse the O(sqrt(n)), they suffer from undefined behavior (UB) and incorrect functionality.
A simple prime test:
// Return true when number is a prime.
bool is_prime(int number) {
// Take care of even values, it is only a bit test.
if (number % 2 == 0) {
return number == 2;
}
// Loop from 3 to square root (n)
for (int test_factor = 3; test_factor <= number / test_factor; test_factor +=
2) {
if (number % test_factor == 0) {
return false;
}
}
return n > 1;
}
Do not use test_factor * test_factor <= number. It risks signed integer overflow (UB) for large primes.
Good compilers see nearby number/test_factor and number % test_factor and emit code that computes both for the about the time cost of one. If still concerned, consider div().
Avoid sqrt(n). Weak floating point libraries do not perform this as exactly as we need for this integer problem, possible returning a value just ever so less than an expected whole number. If still interested in a sqrt(), use lround(sqrt(n)) once before the loop.
Avoid sqrt(n) with wide integer types of n. Conversion of n to a double may lose precision. long double may fair no better.
Test to insure the prime test code does not behave poorly or incorrectly with 1, 0 or any negative value.
Consider bool is_prime(unsigned number) or bool is_prime(uintmax_t number) for extended range.
Avoid testing with candidate factors above the square root n and less than n. Such test factors are never factors of n. Not adhering to this makes for slow code.
A factor is more likely a small value that an large one. Testing small values first is generally far more efficient for non-primes.
Pedantic: Avoid if (number & 1 == 0) {. It is an incorrect test when number < 0 and encoded with rare ones' complement. Use if (number % 2 == 0) { and trust your compiler to emit good code.
More advanced techniques use a list of known/discovered primes and the Sieve of Eratosthenes.
#define TRUE 1
#define FALSE -1
int main()
{
/* Local variables declaration */
int num = 0;
int result = 0;
/* Getting number from user for which max prime quadruplet value is
to be found */
printf("\nEnter the number :");
scanf("%d", &num);
result = Is_Prime( num );
/* Printing the result to standard output */
if (TRUE == result)
printf("\n%d is a prime number\n", num);
else
printf("\n%d is not a prime number\n", num);
return 0;
}
int Is_Prime( int num )
{
int i = 0;
/* Checking whether number is negative. If num is negative, making
it positive */
if( 0 > num )
num = -num;
/* Checking whether number is less than 2 */
if( 2 > num )
return FALSE;
/* Checking if number is 2 */
if( 2 == num )
return TRUE;
/* Checking whether number is even. Even numbers
are not prime numbers */
if( 0 == ( num % 2 ))
return FALSE;
/* Checking whether the number is divisible by a smaller number
1 += 2, is done to skip checking divisibility by even numbers.
Iteration reduced to half */
for( i = 3; i < num; i += 2 )
if( 0 == ( num % i ))
/* Number is divisible by some smaller number,
hence not a prime number */
return FALSE;
return TRUE;
}
I Have Use This Idea For Finding If The No. Is Prime or Not:
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
int x, a;
cout << "Enter The No. :";
cin >> x;
int prime(unsigned int);
a = prime(x);
if (a == 1)
cout << "It Is A Prime No." << endl;
else
if (a == 0)
cout << "It Is Composite No." << endl;
getch();
}
int prime(unsigned int x) {
if (x == 1) {
cout << "It Is Neither Prime Nor Composite";
return 2;
}
if (x == 2 || x == 3 || x == 5 || x == 7)
return 1;
if (x % 2 != 0 && x % 3 != 0 && x % 5 != 0 && x % 7 != 0)
return 1;
else
return 0;
}
if(number%2!=0)
cout<<"Number is prime:"<<endl;
The code is incredibly false. 33 divided by 2 is 16 with reminder of 1 but it's not a prime number...