So I was assigned a three-part program a few days ago and I am having problems with my output. The assignment was to do the following:
1. Create a neat menu that will offer three separate options.
2. Option 1: check whether or not an integer is prime or not. This is checking from 1 to the sqrt(n), not including one but including the sqrt(n).
3. Option 2: display all factors for any given number n in some sort of neat table.
4. Exit the program when option 3 is selected.
We must also use function calls for the menu and options 1 and 2. Must use a switch statement, and can't use any libraries like <math.h> to get our results.
So far I have managed to get all of these right and get everything down and even the approximation for primes without the sqrt function, but when I enter 1 in cmd and try to check if a number is prime, I get nothing and re-looped back into the program. Can anyone help me find what's wrong?
#include <iostream>
usingnamespace std;
int prime_output(int);
void factors_output(int);
void menu();
int main()
{
int selection;
int primes, factors;
do
{
menu();
cin >> selection;
while (selection < 1 || selection > 3)
{
cout << "Please enter a valid menu choice.\n";
cin >> selection;
}
if (selection != 3)
{
switch (selection)
{
case 1:
prime_output(primes=0);
break;
case 2:
factors_output(factors=0);
break;
case 3:
cout << "Thank you for using this program. Goodbye.\n";
break;
default:
cout << "That is not a valid selection.\n";
cout << "Try again.\n";
}
}
} while (selection != 3);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void factors_output(int factors)
{
cout << "Please enter a positive integer greater than zero to retrieve its factors.\n";
cin >> factors;
while (factors <= 1)
{
cout << "Error. Please enter a positive integer greater than zero.";
cin >> factors;
}
cout << "These are the factors for " << factors << ":" << endl;
for (int k = 1; k <= factors; k++)
if (factors % k == 0)
{
cout << "\t" << k;
cout << endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
int prime_output(int primes)
{
int keep_counting = 0;
cout << "Please enter a positive integer greater than 1 to begin.\n";
cin >> primes;
while (primes <= 1)
{
cout << "Error. Please input a positive integer greater than 1.\n";
cin >> primes;
}
int a = primes / 2;
int b = (1 / 2)*((primes / a) + a);
for (int k = 1; k <= b; k++)
{
if (b%k == 0)
{
keep_counting++;
}
if (keep_counting == 2)
{
cout << primes << " is a prime number!\n";
}
else
cout << "This is not a prime number.\n";
} return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
void menu()
{
cout << "Thank you using this Primality and Factor program!\n";
cout << "Remember to use a positive and non-zero integer!\n";
cout << "\t Enter 1 to check if your number is prime or not.\n";
cout << "\t Enter 2 to display all factors for your number.\n";
cout << "\t Enter 3 to exit the application.\n";
}
Thank you so very much! Now I have one more problem. When running the code, I get both output that the number I entered is both prime and non prime. At the same time, I'm getting multiple lines. For example, for 11 I get:
"This is not a prime
This is not a prime
11 is a prime number!"
int prime_output(int primes)
{
int keep_counting = 0;
cout << "Please enter a positive integer greater than 1 to begin.\n";
cin >> primes;
while (primes <= 1)
{
cout << "Error. Please input a positive integer greater than 1.\n";
cin >> primes;
}
int a = primes / 2;
int b = ((primes / a) + a) / 2;
for (int k = 1; k <= b; k++)
{
if (b%k == 0)
{
keep_counting++;
}
if (keep_counting == 2)
{
cout << primes << " is a prime number!\n";
}
else
cout << "This is not a prime number.\n";
} return 0;
}
Look closely at your if statements. The only time that else won't execute is when keep_counting is equal to 2, is that really what you want. Also you may want to consider starting your loop with something other than 1, remember 1 and 2 are "known" prime numbers.
I was speaking in relation to your statement that 1 was a "known" prime number, which was incorrect. Considering what you are saying now, though, perhaps I misunderstood you.
I'm still having problems understanding where I'm going wrong. I understand the concept of checking from 1 until sqrt(n), but isn't "keep_counting" keeping count of my factors that have no remainder? If I am misunderstanding a concept, please guide/help me out some.
Then it would loop to the else statement and return that the number entered is not a prime number?
I'm not trying to be stubborn or wise, but I'm not catching on here..
That's true. When I run the program, I'll get a list (presumably the amount of the entered number) of "This not prime" and then finally the real outcome. So if I enter 11, which is prime, I'll get 11 times telling me it's not prime, with the final one telling me it is.
So adjusting my if, else loop will fix that?
int prime_output(int primes)
{
int keep_counting = 0;
cout << "Please enter a positive integer greater than 1 to begin.\n";
cin >> primes;
while (primes <= 1)
{
cout << "Error. Please input a positive integer greater than 1.\n";
cin >> primes;
}
if (primes == 2)
{
cout << primes << " is a prime number!\n";
}
int a = primes / 2;
int b = ((primes / a) + a) / 2;
for (int k = 1; k <= b; k++)
{
if (primes%k == 0)
{
cout << "This is not a prime number!\n";
}
else
{
cout << "This is a prime number\n";
}
} return 0;
}
Do you realize that you're going to print either is a prime or is not a prime every time through the loop. Perhaps you should also print the number that is generating these messages.
No I am referring to this snippet from the link I provided above.
Suppose we want to find all the prime numbers between 1 and 64. We write out a table of these numbers, and proceed as follows. 2 is the first integer greater than 1, so it is obviously prime. We now cross out all multiples of two.
And then this:
The next number that we haven't crossed out is 3. We circle it and cross out all its multiples.
And then go to the next number left and then the next number left until you reach the square root of your number.
Remember the square root is only used to compute the upper limit of the numbers you will need to check.