Please help me with this C programming challenge Program Obj
Please help me with this C++ programming challenge.
Program Objective
Write a program that asks the user to enter a natural number (non-negative integer) and then determines whether the number is a prime number or not. Then the program asks the user whether he/she wishes to enter another number and repeats the process.
Background
A prime number is a natural number greater than 1 that has only two factors, itself and 1. For example, 2, 3, 5, 7, 11 13, 17, 19, 23 are all prime numbers. An algorithm to determine whether a particular number is a prime number or not, is called a primality test. There are lots of primality tests with some being more efficient (fast or uses few computational resources) and some being more reliable (guaranteed to give the correct answer).
A reliable, but somewhat inefficient, algorithm is the trial division algorithm. To determine if the number n is a prime number, simply divide n by every integer from 2 to n-1. If the remainder of any of these divisions is zero, the number n is not a prime number. It can be shown that one does not need to divide n by integers higher than the largest integer less than or equal to n^(1/2). Also, once a divisor is found for which the remainder is zero, the algorithm can stop immediately, since it is now known that n is not a prime number.
Therefore, the trial division algorithm for determining whether a number n is prime looks like this.
1. Set divisor to 2
2. Divide n by the divisor
3. If the remainder is zero, go to step 7
4. Increment the divisor
5. If the divisor is <= n^(1/2), go to step 2
6. The number is prime, algorithm complete, stop
7. The number is NOT prime, algorithm complete, stop
Program Specifications
Write a program to implement the trial division algorithm.
You program must:
Ask the user to enter a number
Determine if the entered number is prime or not
Tell the user whether the entered number is prime or not
Ask the user whether he/she would like to enter another number to test
If the user responds in the affirmative (types a y or a Y, your program must be able to handle both), start again
The following is an example of correct execution of the program:
Enter an integer greater than 1 to check if it is a prime number: 3
The number 3 is a prime number.
Do you want to enter another number? (y or n): y
Enter an integer greater than 1 to check if it is a prime number: 4
The number 4 is NOT a prime number.
Do you want to enter another number? (y or n): n
Exiting program ...
Algorithm Hints
It is highly recommended that you first design your program with a flowchart.
To implement this algorithm:
use a do-while loop to control the algorithm repetition if the user wants to enter another number to test
use a for loop inside the do-while loop to step through the required divisors
use an if structure inside the for loop to check if the remainder of the division is zero or not (remember the % operator)
the statements in the if structure should include the setting of a flag to indicate whether the division remainder was zero or not, and a break statement to break out of the for loop if the remainder is zero (in that case it has been established that the number is prime, so there is no need to carry on with divisions). The flag status can be used to print out whether the number is prime or not.
You must use exactly the following starter code. Do not by any circumstances change them.
#include <iostream>
using namespace std;
int main()
{
cout << \"Enter an integer greater than 1 to check if it is a prime number: \";
cout << \"The number \" << number << \" is a prime number.\" << endl;
cout << \"The number \" << number << \" is NOT a prime number.\" << endl;
cout << \"Do you want to enter another number? (y or n): \";
cout << \"Exiting program ...\" << endl;
return 0;
}
Solution
#include <iostream>
using namespace std;
int main()
{
int n,flag,number;
char c;
while(true)
{
cout << \"Enter an integer greater than 1 to check if it is a prime number: \";
cin>>n;
flag=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
number = n;
if(flag==0)
{ cout << \"The number \" << number << \" is a prime number.\" << endl;}
else
{cout << \"The number \" << number << \" is NOT a prime number.\" << endl;}
cout << \"Do you want to enter another number? (y or n): \";
cin>>c;
if(c==\'y\')
{
continue;
}
else
{
break;
}
cout << \"Exiting program ...\" << endl;
}
return 0;
}


