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 2024 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.
C Plus Plus Program Example
C++ programming allows you to create a variety of applications, programs, and systems using code. One of the best ways to learn C++ is by analyzing code examples that demonstrate how to perform certain tasks or build specific features. Looking at C++ program examples helps you understand syntax, structure, and programming techniques that you can apply to your projects.
For instance, a classic C++ program example is displaying “Hello World” text. This simple program illustrates basic input/output operations, the use of namespaces like std, return types, the main() function structure, and running/compiling C++ files. Here is an example Hello World program in C++:
// Include iostream library
#include <iostream>
// Main function
int main() {
// Print hello world
std::cout << “Hello World! \n”;
return 0;
}
This program starts by including the iostream library to access input/output functions like cout for printing. It has an main() function that runs when executed. Inside main(), it uses cout from the std namespace to print “Hello World!” text. The return 0; line ends the program.
Other common C++ example programs include math functions, data structures like arrays/lists, simple games, algorithms, taking user input, file handling, object-oriented features with classes, and more. Starting from basic examples is key to building up your C++ skills.
Read More: C Plus Plus Tutorial
C++ Programs List
Basic Examples Of C Plus Plus Program
// 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;
}
Decisions and Loops
// 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++?
C Plus Plus Program Functions
**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;
}
Arrays and Strings C Plus Plus Program
// 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?
C Plus Plus Program Structures
- Storing Information of a Student in a Structure
#include <iostream>
#include <string>
using namespace std;
struct Student {
    string name;
    int rollNo;
    float marks;
};
int main() {
    Student s;
    cout << “Enter student name: “;
    getline(cin, s.name);
    cout << “Enter roll number: “;
    cin >> s.rollNo;
    cout << “Enter marks: “;
    cin >> s.marks;
    cout << “\nStudent Information:” << endl;
    cout << “Name: ” << s.name << endl;
    cout << “Roll Number: ” << s.rollNo << endl;
    cout << “Marks: ” << s.marks << endl;
    return 0;
}
- Adding Two Distances (in inch-feet) System Using Structures
#include <iostream>
using namespace std;
struct Distance {
    int feet;
    float inch;
};
Distance addDistances(Distance d1, Distance d2) {
    Distance sum;
    sum.feet = d1.feet + d2.feet;
    sum.inch = d1.inch + d2.inch;
    if (sum.inch >= 12.0) {
        sum.inch -= 12.0;
        sum.feet++;
    }
    return sum;
}
int main() {
    Distance d1, d2, result;
    cout << “Enter first distance:” << endl;
    cout << “Feet: “;
    cin >> d1.feet;
    cout << “Inches: “;
    cin >> d1.inch;
    cout << “\nEnter second distance:” << endl;
    cout << “Feet: “;
    cin >> d2.feet;
    cout << “Inches: “;
    cin >> d2.inch;
    result = addDistances(d1, d2);
    cout << “\nSum of distances: ” << result.feet << ” feet ” << result.inch << ” inches” << endl;
    return 0;
}
Operator Overloading C Plus Plus Program
// 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;
}
C Plus Plus Program For Practice
Practising make man perfect and we all know that so here are some practice questions for you:
**Basic:**
- Write a program that takes user input and prints a greeting message.
- Write a program that calculates the area and circumference of a circle.
- Write a program that takes two numbers as input and prints the larger of the two.
- Write a program that takes a number as input and prints its factorial.
- Write a program that takes a number as input and checks if it is prime.
**Intermediate:**
- Write a program that takes a string as input and prints it in reverse order.
- Write a program that takes a list of numbers as input and prints the sum of the numbers.
- Write a program that takes a list of numbers as input and prints the largest and smallest numbers in the list.
- Write a program that takes a list of strings as input and prints the longest and shortest strings in the list.
- Write a program that takes a list of strings as input and prints the strings in alphabetical order.
**Advanced:**
- Write a program that takes a list of numbers as input and prints the median of the numbers.
- Write a program that takes a list of numbers as input and prints the mode of the numbers.
- Write a program that takes a list of numbers as input and prints the standard deviation of the numbers.
- Write a program that takes a list of strings as input and prints the strings that contain a given substring.
- Write a program that takes a list of strings as input and prints the strings that are anagrams of a given string.
Therefore, learning C plus plus program opens up endless programming possibilities across many fields like game development, software engineering, finance, and more. This incredible list of top C++ program examples equips you with the key knowledge and skills to start writing efficient C++ code. From basic concepts to advanced data structures and algorithms, these programs demonstrate real-world techniques for building robust C++ apps.Â
To take your C++ skills to the next level, be sure to check out the comprehensive C++ with DSA Course on Physics Wallah. Their expert instructors will guide you through hands-on projects to master C++ in the fastest, most effective way.Â
Enroll now and get started building your career as a top C++ developer!
C Plus Plus Program FAQs
What are the prerequisites for learning C++?
Basic programming knowledge and logic is enough to get started with C++. Familiarity with any programming language like Java or Python is helpful but not mandatory.
How much time does it take to learn C++ properly?
It depends on your prior coding experience, but on average it takes 2-3 months of dedicated practice to get comfortable with C++.
What are the key topics I should focus on?
Core C++, object-oriented programming, STL, data structures and algorithms are critical areas for strong C++ knowledge.
What projects can I build with C++ skills?
You can build games, desktop apps, operating systems, embedded systems, high-frequency trading systems, and physics/engineering simulations with C++.
Is C++ still a good programming language to learn in 2024?
Yes, C++ is still extremely popular and widely used in software development. Learning C++ will open up many job opportunities.