How to find the smallest prime number that is greater than 1000 in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

// Function to check if a number is prime
bool isPrime(int num) {
    if (num <= 1) return false;
    
    for (int i = 2; i <= std::sqrt(num); i++) {
        if (num % i == 0) return false;
    }
    
    return true;
}

int main() {
    int number = 1001; // Start checking from 1001
    
    while (!isPrime(number)) {
        number++; // Increment until a prime is found
    }

    std::cout << "The smallest prime number greater than 1000 is: " << number << std::endl;
}



/*
run:

The smallest prime number greater than 1000 is: 1009

*/

 



answered Nov 5, 2025 by avibootz
...