C++ Create a program that tells the user if the integer is a prime number or not. Please do not use flag or bool.
Check the integer is a prime number or not without using flag or boolean:
code:
#include <iostream>
using namespace std;
int main() {
int i, n; /* variable declaration */
cout <<"Enter a positive integer: ";
cin >> n; /* take user input */
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
cout<<n<<" is not prime"<<endl;
}
else {
for (i = 2; i<n; ++i) { /* run the loop 2 to n */
if (n % i == 0 && i!=n) {
cout<<n<<" is not prime"<<endl;
break;
}
}
}
if (i==n)
cout << n << " is a prime number"; /* print the
output*/
return 0;
}
output screen:
//if you have a any doubt please comment ...........
Get Answers For Free
Most questions answered within 1 hours.