Exactly what I want the program to do:
-User types in an integer (named "number")
-A for loop starts, increasing the integer x by 1 each iteration until "number" divided by x leaves no remainder, then the function FuncNotPrime() will initiate printing "number isn't prime" .
- or if "number" divided by x never gives a remainder 0 then the function FuncPrime to initiate which woild print "number is prime".
What is wrong:
Ok so it works when I type in a non-prime, but for some reason i don't understand it doesn't work when i type in a prime number.
Any help would be great because I'm getting frustrated now as I'm almost certain the code is right. I understand this probably isn't the standard way for checking for primes but I'm really more concerned why the code isn't wokring.
Many thanks
Jack
- Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;
int number;
void FuncNotPrime(int);
void FuncPrime(int);
int main()
{
cout << "Welcome to the prime number tester.\nPlease enter an integer number:";
cin >> number;
for (int x=2;;x++)
{
if (number%x==0)
FuncNotPrime(number);
break;
if (number==x+1)
FuncPrime(number);
break;
}
char f;
cin >> f;
return 0;
}
void FuncNotPrime(int y)
{
cout << y << ' ' << "is not prime";
}
void FuncPrime(int y)
{
cout << y << ' ' << "is prime";
}
