
C plus plus program learning is the best way to start as a beginner. As we all know C++ is one of the most popular and widely used programming languages. As a C++ expert programmer, you can design or develop operating systems, video games, and scientific programs. Therefore, learning C++ properly helps you open up many programming opportunities in areas like game development, finance, science, and more. With 2025 now underway, it's a great time to level up your C++ skills by learning from the best C++ program examples.
This incredible list, with top C++ sample programs and projects spanning key computer science topics like data structures, algorithms, and object-oriented programming while touching on useful application areas like game and software development. So whether you are a coding beginner or a seasoned programming expert, these C++ code examples will help take your skills to the next level.
If you're looking for the fastest way to master C++ through hands-on practice, be sure to check out C++ with DSA Course on Physics Walla. Over 10+ hours of premium video tutorials walk you through building impressive C++ apps from the ground up - one of the best investments you can make this year for unlocking C++ jobs and advancement.
Now let's jump in and explore the top C plus plus program examples you should add to your learning list.
Read More: C Plus Plus Tutorial
// 1. Display "Hello World"
#include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; }// 2. Read and Print the User Input Number
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << "You entered: " << num; return 0; }// 3. Add Two Integer Numbers
#include <iostream> using namespace std; int main() { int num1, num2, sum; cout << "Enter two numbers: "; cin >> num1 >> num2; sum = num1 + num2; cout << num1 << " + " << num2 << " = " << sum; return 0; }// 4. Find Quotient and Remainder of Division
#include <iostream> using namespace std; int main() { int dividend, divisor, quotient, remainder; cout << "Enter dividend: "; cin >> dividend; cout << "Enter divisor: "; cin >> divisor; quotient = dividend / divisor; remainder = dividend % divisor; cout << "Quotient = " << quotient << endl; cout << "Remainder = " << remainder; return 0; }// 5. Find ASCII Value of Character
#include <iostream> using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; int ascii = int(ch); cout << "The ASCII value of " << ch << " is " << ascii; return 0; }// 6. Multiply Two Numbers
#include <iostream> using namespace std; int main() { int num1, num2, product; cout << "Enter two numbers: "; cin >> num1 >> num2; product = num1 * num2; cout << num1 << " x " << num2 << " = " << product; return 0; }// 1. Check Even or Odd Number
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; if(num % 2 == 0) cout << num << " is even"; else cout << num << " is odd"; return 0; }// 2. Check Alphabet Vowel or Consonant
#include <iostream> using namespace std; int main() { char ch; cout << "Enter an alphabet: "; cin >> ch; if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { cout << ch << " is a vowel"; } else { cout << ch << " is a consonant"; } return 0; }// 3. Find the Largest of Three Numbers
#include <iostream> using namespace std; int main() { int num1, num2, num3; cout << "Enter three numbers: "; cin >> num1 >> num2 >> num3; if(num1 >= num2 && num1 >= num3) cout << num1 << " is the largest number"; if(num2 >= num1 && num2 >= num3) cout << num2 << " is the largest number"; if(num3 >= num1 && num3 >= num2) cout << num3 << " is the largest number"; return 0; }// 4. Solve Quadratic Equation
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, c; float root1, root2; // Take coefficients cout << "Enter coefficients a, b and c: "; cin >> a >> b >> c; // Calculate roots root1 = (-b + sqrt(b*b - 4*a*c)) / (2*a); root2 = (-b - sqrt(b*b - 4*a*c)) / (2*a); // Print roots cout << "Root 1 = " << root1 << endl; cout << "Root 2 = " << root2; return 0; }// 5. Calculate Sum of Natural Numbers
#include <iostream> using namespace std; int main() { int n, sum = 0; cout << "Enter a positive integer: "; cin >> n; for(int i = 1; i <= n; ++i) { sum += i; } cout << "Sum = " << sum; return 0; }// 6. Check Leap Year
#include <iostream> using namespace std; int main() { int year; cout << "Enter a year: "; cin >> year; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) cout << year << " is a leap year."; else cout << year << " is not a leap year."; } else cout << year << " is a leap year." ; } else { cout << year << " is not a leap year."; } return 0; }// 7. Find Factorial
#include <iostream> using namespace std; int factorial(int n) { if(n > 1) return n * factorial(n - 1); else return 1; } int main() { int num; cout << "Enter a positive integer: "; cin >> num; cout << "Factorial of " << num << " = " << factorial(num); return 0; }// 8. Generate Multiplication Table
#include <iostream> using namespace std; int main() { int num, i; cout << "Enter an integer: "; cin >> num; for(i=1; i<=10; ++i) { cout << num << " * " << i << " = " << num*i << endl; } return 0; }// 9. Display Fibonacci Series
#include <iostream> using namespace std; int main() { int n, t1 = 0, t2 = 1, nextTerm; cout << "Enter the number of terms: "; cin >> n; cout << "Fibonacci Series: "; for (int i = 1; i <= n; ++i) { // Prints the first two terms. if(i == 1) { cout << t1 << ", "; continue; } if(i == 2) { cout << t2 << ", "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; cout << nextTerm << ", "; } return 0; }// 10. Find GCD of two numbers
#include <iostream> using namespace std; int findGCD(int x, int y) { if (y == 0) return x; else return findGCD(y, x % y); } int main() { int n1, n2; cout << "Enter two integers: "; cin >> n1 >> n2; cout << "GCD of " << n1 << " and " << n2 << " = " << findGCD(n1, n2); return 0; }// 11. Find LCM of two numbers
#include <iostream> using namespace std; int findLCM(int x, int y) { if(x > y) return (x*y)/findGCD(x,y); else return (x*y)/findGCD(y,x); } int findGCD(int x, int y) { if (y == 0) return x; else return findGCD(y, x % y); } int main() { int n1, n2; cout << "Enter two numbers: "; cin >> n1 >> n2; cout << "LCM of " << n1 << " and " << n2 << " = " << findLCM(n1, n2); return 0; }// 12. Reverse a Number
#include <iostream> using namespace std; int main() { int n, reversed = 0; cout << "Enter an integer: "; cin >> n; while(n != 0) { int digit = n % 10; reversed = reversed * 10 + digit; n /= 10; } cout << "Reversed Number: " << reversed; return 0; }// 13. Calculate Power of a Number
#include <iostream> #include <cmath> using namespace std; int main() { double base, exponent; double result; cout << "Enter base number: "; cin >> base; cout << "Enter exponent : "; cin >> exponent; result = pow(base,exponent); cout << base << "^" << exponent<< " = "<< result; return 0; }// 14. Check Palindrome Number
#include <iostream> using namespace std; int main() { int num, reversedNum = 0, remainder, originalNum; cout << "Enter an integer: "; cin >> num; originalNum = num; // reversed integer is stored in reversedNum while( num != 0 ) { remainder = num % 10; reversedNum = reversedNum * 10 + remainder; num /= 10; } // palindrome if originalNum and reversedNum are equal if (originalNum == reversedNum) cout << originalNum << " is a palindrome."; else cout << originalNum << " is not a palindrome."; return 0; }// 15. Check Prime Number
#include <iostream> using namespace std; int main() { int num, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> num; // 0 and 1 are not prime numbers if (num == 0 || num == 1) { isPrime = false; } else { for(i = 2; i <= num/2; ++i) { if(num % i == 0) { isPrime = false; break; } } } if (isPrime) cout << num << " is a prime number"; else cout << num << " is not a prime number"; return 0; }// 16. Display Prime Numbers in Interval
#include <iostream> #include <math.h> using namespace std; int main() { int low, high; bool flag; cout << "Enter lower interval: "; cin >> low; cout << "Enter upper interval: "; cin >> high; for(int i = low; i <= high; i++) { if(i == 1 || i == 0) continue; flag = 1; for(int j = 2; j <= sqrt(i); j++) { if(i % j == 0) { flag = 0; break; } } if(flag == 1) cout << i << " "; } return 0; }// 17. Check Armstrong Number
#include <iostream> #include <math.h> using namespace std; int main() { int num, originalNum, remainder, n = 0, result = 0; cout << "Enter a positive integer: "; cin >> num; originalNum = num; // Count number of digits while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; // Calculate result while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } if(result == num) cout << num << " is an Armstrong number."; else cout << num << " is not an Armstrong number."; return 0; }// 18. Display Armstrong Numbers in Interval
#include <bits/stdc++.h> using namespace std; bool isArmstrong(int n) { int temp, digits = 0, last = 0, sum = 0; temp = n; while (temp > 0) { digits++; temp = temp/10; } temp = n; while (temp > 0) { last = temp % 10; sum += pow(last, digits); temp = temp/10; } if(n == sum) return true; return false; } int main() { int low, high; cout<<"Enter lower interval: "; cin>>low; cout<<"Enter upper interval: "; cin>>high; for(int i=low;i<=high;i++) { if(isArmstrong(i)) cout<< i<<" "; } return 0; }Read More: What is the meaning of 1LL in C++?
**1. C++ Program to Display Prime Numbers Between Two Intervals Using Functions**
```c++ #include <iostream> using namespace std; // Function to check if a number is prime bool isPrime(int n) { if (n < 2) { return false; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; } // Function to display prime numbers between two intervals void displayPrimeNumbers(int start, int end) { for (int i = start; i <= end; i++) { if (isPrime(i)) { cout << i << " "; } } cout << endl; } int main() { int start, end; cout << "Enter the starting and ending intervals: "; cin >> start >> end; displayPrimeNumbers(start, end); return 0; } ```**2. C++ Program to Check Prime Number By Creating a Function**
```c++ #include <iostream> using namespace std; // Function to check if a number is prime bool isPrime(int n) { if (n < 2) { return false; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; } int main() { int num; cout << "Enter a number: "; cin >> num; if (isPrime(num)) { cout << num << " is a prime number." << endl; } else { cout << num << " is not a prime number." << endl; } return 0; } ```**3. C++ Program to Check Whether a Number can be Express as Sum of Two Prime Numbers**
```c++ #include <iostream> using namespace std; // Function to check if a number is prime bool isPrime(int n) { if (n < 2) { return false; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; } // Function to check if a number can be expressed as sum of two prime numbers bool canBeExpressedAsSumOfTwoPrimes(int n) { for (int i = 2; i <= n / 2; i++) { if (isPrime(i) && isPrime(n - i)) { return true; } } return false; } int main() { int num; cout << "Enter a number: "; cin >> num; if (canBeExpressedAsSumOfTwoPrimes(num)) { cout << num << " can be expressed as sum of two prime numbers." << endl; } else { cout << num << " cannot be expressed as sum of two prime numbers." << endl; } return 0; } ```**4. C++ program to Find Sum of Natural Numbers using Recursion**
```c++ #include <iostream> using namespace std; // Function to calculate sum of natural numbers using recursion int sumOfNaturalNumbers(int n) { if (n == 0) { return 0; } else { return n + sumOfNaturalNumbers(n - 1); } } int main() { int num; cout << "Enter a number: "; cin >> num; cout << "Sum of natural numbers from 1 to " << num << " is: " << sumOfNaturalNumbers(num) << endl; return 0; } ```**5. C++ program to Calculate Factorial of a Number Using Recursion**
```c++ #include <iostream> using namespace std; // Function to calculate factorial of a number using recursion int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } int main() { int num; cout << "Enter a number: "; cin >> num; cout << "Factorial of " << num << " is: " << factorial(num) << endl; return 0; } ```**6. C++ Program to Find G.C.D Using Recursion**
```c++ #include <iostream> using namespace std; // Function to calculate G.C.D of two numbers using recursion int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int main() { int num1, num2; cout << "Enter two numbers: "; cin >> num1 >> num2; cout << "G.C.D of " << num1 << " and " << num2 << " is: " << gcd(num1, num2) << endl; return 0; }// 1. C++ program to calculate average of numbers using arrays
#include <iostream> using namespace std; int main() { // Declare an array to store the numbers int numbers[5]; // Get the numbers from the user cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) { cin >> numbers[i]; } // Calculate the sum of the numbers int sum = 0; for (int i = 0; i < 5; i++) { sum += numbers[i]; } // Calculate the average of the numbers float average = (float)sum / 5; // Print the average cout << "The average of the numbers is: " << average << endl; return 0; }// 2. C++ program to find largest element of an array
#include <iostream> using namespace std; int main() { // Declare an array to store the numbers int numbers[5]; // Get the numbers from the user cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) { cin >> numbers[i]; } // Find the largest number in the array int largest = numbers[0]; for (int i = 1; i < 5; i++) { if (numbers[i] > largest) { largest = numbers[i]; } } // Print the largest number cout << "The largest number in the array is: " << largest << endl; return 0; }Read More: 0 in C++: How does the -'0' and +'0' work in C?
// 1. Increment/Decrement Operator Overloading
#include <iostream> using namespace std; class Test { private: int x; public: Test(): x(0) {} void operator++() { ++x; } void operator--() { --x; } int getX() { return x; } }; int main() { Test t; ++t; // Calls t.operator++() cout << t.getX() << endl; // Prints 1 --t; // Calls t.operator--() cout << t.getX() << endl; // Prints 0 return 0; }// 2. Subtracting Complex Numbers using Operator Overloading
#include <iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) { real = r; imag = i; } // Overload - operator Complex operator-(Complex const &obj) { Complex res; res.real = real - obj.real; res.imag = imag - obj.imag; return res; } void print() { cout << real << " + i" << imag << endl; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 - c2; // c3 calls c1.operator-() c3.print(); // Prints 8 + i1 return 0; }