{}
run-icon
main.cpp
#include <iostream> using namespace std; bool isPrime(int num) { if (num < 2) return false; // Numbers less than 2 are not prime for (int i = 2; i * i <= num; i++) { // Check divisibility up to square root of num if (num % i == 0) return false; // If divisible, not prime } return true; // num is prime } void findPrimes(int n) { cout << "Prime Numbers: "; for (int i = 1; i <= n; i++) { if (isPrime(i)) cout << i << " "; // Output the prime number } cout << endl; } int main() { int n; cout << "Enter value of n: "; cin >> n; findPrimes(n); // Find and print all primes up to n return 0; }
Output