Program to print first 10 prime numbers Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to print the first 10 prime numbers.Note: A number N is said to be prime if it has exactly two factors i.e. 1 and the number itself N Output Format:2, 3, 5, 7, 9...Approach:Prime Test: To check whether a number N is prime we can check its divisibility with each number from 2 to N - 1, If it is divisible by any number in this range, we can conclude that N is not a prime number.Looping until first 10 primes are not found: We can use a while loop to continue our prime check on the numbers until we print the first 10 prime numbers.Step-by-step algorithm:Maintain a variable cnt = 0 to keep track of number of primes printed so far.Maintain a variable num = 2 to keep track of the number to be checked for prime.Run a loop till cnt is less than 10.If num is prime, increment cnt by 1.Increment num by 1.After cnt becomes 10, we have printed the first 10 prime numbers.Below is the implementation of the above approach: C++ #include <iostream>; using namespace std; // Function to check if a number is prime or not bool isPrime(int N) { for (int i = 2; i < N; i++) { if (N % i == 0) return false; } return true; } int main() { // Variable to store number of primes printed so far int cnt = 0; // Variable to store the number to be checked for prime int num = 2; // Iterate till we have printed the first 10 primes while (cnt < 10) { // Prime Check if (isPrime(num)) { cout << num << endl; cnt++; } num++; } } Java public class PrimeNumbers { // Function to check if a number is prime or not public static boolean isPrime(int N) { for (int i = 2; i < N; i++) { if (N % i == 0) { return false; } } return true; } // Main function public static void main(String[] args) { // Variable to store the number of primes printed so far int cnt = 0; // Variable to store the number to be checked for prime int num = 2; // Iterate until we have printed the first 10 primes while (cnt < 10) { // Prime Check if (isPrime(num)) { System.out.println(num); cnt++; } num++; } } } Python # Function to check if a number is prime or not def is_prime(N): for i in range(2, N): if N % i == 0: return False return True # Main function def main(): # Variable to store the number of primes printed so far cnt = 0 # Variable to store the number to be checked for prime num = 2 # Iterate until we have printed the first 10 primes while cnt < 10: # Prime Check if is_prime(num): print(num) cnt += 1 num += 1 # Run the main function if __name__ == "__main__": main() C# using System; class Program { // Function to check if a number is prime or not static bool IsPrime(int N) { for (int i = 2; i < N; i++) { if (N % i == 0) return false; } return true; } static void Main() { // Variable to store the number of primes printed so far int cnt = 0; // Variable to store the number to be checked for prime int num = 2; // Iterate until we have printed the first 10 primes while (cnt < 10) { // Prime Check if (IsPrime(num)) { Console.WriteLine(num); cnt++; } num++; } } } JavaScript // Function to check if a number // is prime or not function isPrime(N) { for (let i = 2; i < N; i++) { if (N % i === 0) return false; } return true; } // Variable to store number of // primes printed so far let cnt = 0; // Variable to store the number // to be checked for prime let num = 2; // Iterate till we have printed // the first 10 primes while (cnt < 10) { // Prime Check if (isPrime(num)) { console.log(num); cnt++; } num++; } Output2 3 5 7 11 13 17 19 23 29 Time Complexity: O(1) if we consider only fixed number of elements like 10. If number of elements are provided by the user, then it would be O(N^2) and can be optimized using Sieve AlgorithmAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to print first 10 prime numbers V vaibhav_gfg Follow Improve Article Tags : DSA loop Similar Reads Program to print prime numbers from 1 to N. Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i 15+ min read Program to Print First N Natural Numbers Counting numbers like 1, 2, 3, 4, 5, 6 ⦠Basically, all integers greater than 0 are natural numbers. Facts about Natural numbers The natural numbers are the ordinary numbers, 1, 2, 3, etc., with which we count.The next possible natural number can be found by adding 1 to the current natural numberThe 4 min read Program to find the Nth Prime Number Given an integer N. The task is to find the Nth prime number. Examples: Input : 5 Output : 11 Input : 16 Output : 53 Input : 1049 Output : 8377 Approach: Find the prime numbers up to MAX_SIZE using Sieve of Eratosthenes.Store all primes in a vector.For a given number N, return the element at (N-1)th 12 min read Program to find the next prime number Given an integer N. The task is to find the next prime number i.e. the smallest prime number greater than N. Examples: Input: N = 10 Output: 11 11 is the smallest prime number greater than 10. Input: N = 0 Output: 2 Approach: First of all, take a boolean variable found and initialize it to false.Now 5 min read Print prime numbers from 1 to N in reverse order Given a number N, print all prime number smaller than or equal to N in reverse order . For example, if N is 9, the output should be â7, 5, 3, 2â. Examples: Input : N = 5Output : 5 3 2 Input : N = 20Output : 19 17 13 11 7 5 3 2 A simple solution is to traverse from N to 1. For every number, check if 7 min read Like