Introduction to C++:
C++ is a programming language used to create computer programs. It is one of the most
popular and powerful languages used in the world today.
C++ is used to write instructions for the computer so it can perform specific tasks.
It was developed by Bjarne Stroustrup in 1983.
C++ is an extension of the C language, which means it has all the features of C, plus
some new ones.
Why do we use C++?
To write games, software, operating systems, and apps.
It is a fast and efficient language.
It supports Object-Oriented Programming (OOP), which makes code easy to
understand and reuse.
Basic Structure of a C++ Program:
#include<iostream>
using namespace std;
int main() {
cout << "Hello, world!";
return 0;
#include<iostream> → Tells the program to include input/output functions.
Using namespace std; → Lets us use standard C++ functions without writing
std:: every time.
Int main() → This is the main function, where the program starts.
Cout << “Hello, world!”; → Displays the text on the screen.
Return 0; → Ends the program.
Basic Programs to Practice:
Topic: input/output handling
Operators
Basic Geometrical shapes
Program 1: Take Three Numbers and Print Sum, Product, and
Average
#include<iostream>
Using namespace std;
int main() {
int a, b, c;
cout << “Enter three numbers: “;
cin >> a >> b >> c;
int sum = a + b + c;
int product = a * b * c;
float average = (a + b + c) / 3.0;
Cout << “Sum = “ << sum << endl;
Cout << “Product = “ << product << endl;
Cout << “Average = “ << average << endl;
Return 0;
}
Program 2: Calculate Area and Perimeter of a Rectangle
#include<iostream>
Using namespace std;
Int main() {
Float length, width;
Cout << “Enter length and width of the rectangle: “;
Cin >> length >> width;
Float area = length * width;
Float perimeter = 2 * (length + width);
Cout << “Area = “ << area << endl;
Cout << “Perimeter = “ << perimeter << endl;
Return 0;
Program 3: Calculate Area and Circumference of a Circle
#include<iostream>
Using namespace std;
Int main() {
Float radius;
Const float pi = 3.1416;
Cout << “Enter radius of the circle: “;
Cin >> radius;
Float area = pi * radius * radius;
Float circumference = 2 * pi * radius;
Cout << “Area = “ << area << endl;
Cout << “Circumference = “ << circumference << endl;
Return 0;
Program4: Calculate Area of a Triangle
#include<iostream>
Using namespace std;
Int main() {
Float base, height;
Cout << “Enter base and height of the triangle: “;
Cin >> base >> height;
Float area = 0.5 * base * height;
Cout << “Area of Triangle = “ << area << endl;
Return 0;
Program 5: Find Square and Cube of a Number
#include<iostream>
Using namespace std;
Int main() {
Int number;
Cout << “Enter a number: “;
Cin >> number;
Int square = number * number;
Int cube = number * number * number;
Cout << “Square = “ << square << endl;
Cout << “Cube = “ << cube << endl;
Return 0;
Program 6: Convert Temperature from Celsius to Fahrenheit
#include<iostream>
Using namespace std;
Int main() {
Float celsius, fahrenheit;
Cout << “Enter temperature in Celsius: “;
Cin >> celsius;
Fahrenheit = (celsius * 9 / 5) + 32;
Cout << “Temperature in Fahrenheit = “ << fahrenheit << endl;
Return 0;
Program 7: Calculate Area of a Square
#include<iostream>
using namespace std;
int main() {
float side, area;
cout << "Enter the length of the side of the square: ";
cin >> side;
area = side * side;
cout << "Area of the square = " << area << endl;
return 0;
Topics: Control Structures (Conditional & Repetitional)
Program 8: Check the Greatest Among Three Numbers
#include<iostream>
Using namespace std;
Int main() {
Int a, b, c;
Cout << “Enter three numbers: “;
Cin >> a >> b >> c;
If (a >= b && a >= c)
Cout << a << “ is the greatest.” << endl;
Else if (b >= a && b >= c)
Cout << b << “ is the greatest.” << endl;
Else
Cout << c << “ is the greatest.” << endl;
Return 0;
}
Program 9: Check Even or Odd
#include<iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << "The number is Even." << endl;
else
cout << "The number is Odd." << endl;
return 0;
Program 10: Check Positive, Negative or Zero
#include<iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << "Positive number" << endl;
else if (num < 0)
cout << "Negative number" << endl;
else
cout << "Zero" << endl;
return 0;
Program 11: Check Vowel or Consonant
#include<iostream>
Using namespace std;
Int main() {
Char ch;
Cout << “Enter an alphabet: “;
Cin >> ch;
Ch = tolower(ch); // make it lowercase for easy checking
If (ch == ‘a’ || ch == ‘e’ || ch == ‘I’ || ch == ‘o’ || ch == ‘u’)
Cout << “It is a Vowel.” << endl;
Else if ((ch >= ‘a’ && ch <= ‘z’))
Cout << “It is a Consonant.” << endl;
Else
Cout << “Not an alphabet.” << endl;
Return 0;
Program 12: Check Prime or Composite
#include<iostream>
Using namespace std;
Int main() {
Int num, I, count = 0;
Cout << “Enter a number: “;
Cin >> num;
If (num <= 1) {
Cout << “Neither Prime nor Composite” << endl;
} else {
For (I = 2; I <= num / 2; i++) {
If (num % I == 0) {
Count++;
Break;
If (count == 0)
Cout << “Prime Number” << endl;
Else
Cout << “Composite Number” << endl;
Return 0;
Program 13: Grade Calculation
#include<iostream>
Using namespace std;
Int main() {
Int marks;
Cout << “Enter your marks (0 to 100): “;
Cin >> marks;
If (marks >= 90)
Cout << “Grade: A+” << endl;
Else if (marks >= 80)
Cout << “Grade: A” << endl;
Else if (marks >= 70)
Cout << “Grade: B” << endl;
Else if (marks >= 60)
Cout << “Grade: C” << endl;
Else if (marks >= 50)
Cout << “Grade: D” << endl;
Else
Cout << “Fail” << endl;
Return 0;
Program 14: Check Character Type (Uppercase, Lowercase, Digit, or
Symbol)
#include<iostream>
Using namespace std;
Int main() {
Char ch;
Cout << “Enter any character: “;
Cin >> ch;
If (ch >= ‘A’ && ch <= ‘Z’)
Cout << “Uppercase Letter” << endl;
Else if (ch >= ‘a’ && ch <= ‘z’)
Cout << “Lowercase Letter” << endl;
Else if (ch >= ‘0’ && ch <= ‘9’)
Cout << “Digit” << endl;
Else
Cout << “Symbol” << endl;
Return 0;
Program 15: Switch Statement – Simple Calculator
#include<iostream>
Using namespace std;
Int main() {
Char op;
Float a, b;
Cout << “Enter operator (+, -, *, /): “;
Cin >> op;
Cout << “Enter two numbers: “;
Cin >> a >> b;
Switch (op) {
Case ‘+’:
Cout << “Sum = “ << a + b << endl;
Break;
Case ‘-‘:
Cout << “Difference = “ << a – b << endl;
Break;
Case ‘*’:
Cout << “Product = “ << a * b << endl;
Break;
Case ‘/’:
If (b != 0)
Cout << “Quotient = “ << a / b << endl;
Else
Cout << “Error: Division by zero” << endl;
Break;
Default:
Cout << “Invalid Operator” << endl;
Return 0;
Also do this using switch and if else if
1-Display day of week by number (1-7).
2-Display month name using month number.
Program 16: Print Counting from 1 to 100 (using for loop)
#include<iostream>
Using namespace std;
Int main() {
For (int I = 1; I <= 100; i++) {
Cout << I << “ “;
}
Return 0;
Program 17: Print Even Numbers from 1 to 100 (using while loop)
#include<iostream>
Using namespace std;
Int main() {
Int I = 2;
While (I <= 100) {
Cout << I << “ “;
I += 2;
Return 0;
Program 19: Print Sum of Series (e.g., 1 + 2 + 3 + … + n, using
while loop)
#include<iostream>
Using namespace std;
Int main() {
Int n, sum = 0, I = 1;
Cout << “Enter value of n: “;
Cin >> n;
While (I <= n) {
Sum += I;
I++;
Cout << “Sum = “ << sum << endl;
Return 0;
Program 20: Print Multiplication Table (using for loop)
#include<iostream>
Using namespace std;
Int main() {
Int num;
Cout << “Enter a number: “;
Cin >> num;
For (int I = 1; I <= 10; i++) {
Cout << num << “ x “ << I << “ = “ << num * I << endl;
Return 0;
Program 21: Factorial of a Number Using while Loop
#include<iostream>
Using namespace std;
Int main() {
Int num, fact = 1, I = 1;
Cout << “Enter a number: “;
Cin >> num;
While (I <= num) {
Fact *= I; // same as: fact = fact * i
I++;
Cout << “Factorial = “ << fact << endl;
Return 0;
Example:
If the user enters 5, the steps are:
Fact = 1 × 1 = 1
Fact = 1 × 2 = 2
Fact = 2 × 3 = 6
Fact = 6 × 4 = 24
Fact = 24 × 5 = 120
So, the output is:
Factorial = 120
Topic: Functions
Program 22: Function to Display a Welcome Message
#include<iostream>
using namespace std;
void displayMessage() {
cout << "Welcome to C++ Programming!" << endl;
}
int main() {
displayMessage(); // Function call
return 0;
Program 23: Function to Check Even or Odd
#include<iostream>
Using namespace std;
Void checkEvenOdd(int n) {
If (n % 2 == 0)
Cout << n << “ is Even.” << endl;
Else
Cout << n << “ is Odd.” << endl;
Int main() {
Int num;
Cout << “Enter a number: “;
cin >> num;
checkEvenOdd(num); // Function call
return 0;
Program 24: Function to Add Two Numbers
#include<iostream>
Using namespace std;
// Function declaration
Int add(int a, int b) {
Return a + b;
Int main() {
Int x, y;
Cout << “Enter two numbers: “;
Cin >> x >> y;
Int result = add(x, y); // Function call
Cout << “Sum = “ << result << endl;
Return 0;