Introduction

Prime numbers are numbers greater than 1 that have no divisors other than 1 and themselves. Generating prime numbers is a fundamental concept in computer science and mathematics. This program demonstrates how to generate a list of prime numbers up to a given limit using C++ programming language.

Objective

The objective of this program is to generate a list of prime numbers up to a specified limit input by the user. The program will utilize a straightforward algorithm that checks each number for primality and outputs all prime numbers in the given range.

Prime Number Generator Code in C++

#include 
#include 
using namespace std;

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

// Function to generate and print prime numbers up to the given limit
void generatePrimes(int limit) {
    cout << "Prime numbers up to " << limit << " are:\n";
    for (int i = 2; i <= limit; ++i) {
        if (isPrime(i)) {
            cout << i << " ";
        }
    }
    cout << endl;
}

int main() {
    int limit;

    // Ask user for the limit
    cout << "Enter the limit up to which you want to generate prime numbers: "; cin >> limit;

    // Generate primes up to the specified limit
    generatePrimes(limit);

    return 0;
}

Explanation of the Program Structure

This C++ program consists of the following key components:

  • isPrime Function: This function takes an integer as input and checks whether it is a prime number. It does so by attempting to divide the number by all integers from 2 up to the square root of the number. If any division results in a remainder of zero, the number is not prime.
  • generatePrimes Function: This function iterates through numbers from 2 to the user-specified limit and calls the isPrime function for each number. If the number is prime, it is printed.
  • main Function: The main function asks the user to enter a limit for prime number generation, then calls the generatePrimes function to display the primes up to that limit.

How to Run the Program

Follow these steps to run the program:

  1. Copy the provided code into a new C++ file (e.g., prime_generator.cpp).
  2. Open a terminal or command prompt and navigate to the folder where your file is located.
  3. Compile the program using a C++ compiler. For example, if you’re using g++, run the command:
    g++ prime_generator.cpp -o prime_generator
  4. Run the compiled program:
    ./prime_generator
  5. The program will ask you to enter a limit, and it will then display all prime numbers up to that limit.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)