This C++ program takes a positive integer as input from the user and determines whether it is a prime number or not. It uses a for loop to check if the input number is divisible by any number between 2 and half of the input number. If it is divisible, it sets a boolean flag to false and breaks out of the loop. After the loop, it checks the boolean flag to print whether the number is prime or not.
This C++ program takes a positive integer as input from the user and determines whether it is a prime number or not. It uses a for loop to check if the input number is divisible by any number between 2 and half of the input number. If it is divisible, it sets a boolean flag to false and breaks out of the loop. After the loop, it checks the boolean flag to print whether the number is prime or not.
This C++ program takes a positive integer as input from the user and determines whether it is a prime number or not. It uses a for loop to check if the input number is divisible by any number between 2 and half of the input number. If it is divisible, it sets a boolean flag to false and breaks out of the loop. After the loop, it checks the boolean flag to print whether the number is prime or not.
#include <iostream> using namespace std; int main() { int n, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; if(n<=1) { cout<<n" is not prime number"; } else { for(i = 2; i <= n / 2; ++i) { if(n % i == 0) { isPrime = false; break; } } if (isPrime) cout <<n<<" is a prime number"; else cout <<n<<" is not a prime number"; } return 0; }