Finding Prime Numbers in C++

Each programming language chapter in this book has a pseudo-capstone project associated with it. The Java chapter’s project involves downloading weather data. The Python chapter has you make a Twitter bot to tweet about the current weather, getting data from a weather API in order to do so. The web front-end chapter has you make a simple non-interactive website. The purpose of these projects is to combine many different language features that were taught earlier in the chapter, and to give you some ideas for how these things can be used for useful and complicated programs, as opposed to the earlier code example which generally only concentrate on a single feature. The end-of-the-chapter project really ties everything together. Feel free to use these as your own projects, starting with the code from this book, but maybe you want to customize it to suit your own purposes.

This section goes over how to make a simple program that can figure out if numbers are prime or not. This project combines a lot of different features of C++.

Here’s the source code for a prime-finding program that saves primes to a text file:

#include <iostream>

#include <fstream>

using namespace std;

bool numIsPrime(unsigned long long argNum){

for (unsigned long long x = 2; x < argNum; x++) {

if (argNum % x == 0) {

return false;



return true;


int main() {

//finds primes in the first 100,000 positive natural numbers

ofstream outputFile;“output.dat”);

for (unsigned long long i = 2; i < 100000; i++) {

if (numIsPrime(i)) {

outputFile << i << endl;



return 0;


The above code demonstrates functions, file IO, and control flow in C++.

There is a more advanced version of this program on GitHub here (consisting of C++, Bash, and gnuplot script):

It can find prime numbers and the gaps between primes, and saves them to files as comma-separated values so that they can be used as x,y coordinates in a graph about prime number values and the gaps between subsequent primes. The program can even save its progress and resume even if it was quit unexpectedly. It can also measure the frequency of primes (long story short, the bigger the number, the less likely it is to be prime). Additionally, it takes the saved data and then uses gnuplot to automatically generate graphs, like this one:

Writing code is good, but it can be useful to read code too. In my experiences, I’ve found that it’s harder to read code than to write it, especially if you’re reading someone else’s code. It’s easy for you to understand your own thought process, but reading someone else’s code requires trying to get into their mindset.

That being said, I think it’s worth pointing out that the method I used for finding prime numbers is called a naïve algorithm. That means it’s easy to understand and easy to code, but not necessarily the most efficient in terms of performance. There are some searching and sorting algorithms that are like this too. There are faster ways to find prime numbers (in terms of the code running, not how fast it takes to write it), but the algorithms for doing it in a more efficient way are more complicated and sometimes harder to code. The point here is that the simplest and most obvious solution isn’t necessarily the best.

← Previous | Next →

C++ Topic List

Main Topic List

Leave a Reply

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