C++ Program to Print Prime Numbers Between 1 to n using For Loop
Write C++ Program to Print Prime Numbers Between 1 to n using For Loop
// CPP Program to Print Prime Numbers Between 1 to n using For Loop #include <iostream> #include <stdbool.h> using namespace std; bool isPrime(int n) { if(n == 1 || n ==0) return false; int i = 2; for(i = 2; i < n; i++) { if(n % i == 0) { return false; } } return true; } int main() { int n, i; cout << "Enter the number :--> "; cin >> n; cout << "Prime numbers between 1 to " << n << " :--> "; for(i = 1; i < n; i++) { if(isPrime(i)) { cout << i << " "; } } return 0; }
Output:
Enter the number :--> 25
The prime numbers are :2 3 5 7 11 13 17 19 23