Welcome to the 100-plus-cpp-programs repository! This collection contains over 100 C++ programs, designed to cover a wide range of topics from basic concepts to more advanced techniques. Whether you're a beginner or an experienced programmer looking to enhance your skills, this repository offers valuable resources for learning and practice.
-
Display Information:
Write a C++ program that displays your name, department, and section on the console. Ensure that each detail appears on a new line using the escape sequence\n
. -
Favorite Personality Quote:
Write a C++ program that displays the name and quote of your favorite personality. Use the escape sequence\"
to put the quote in double quotation marks. Example output:
Friedrich Nietzsche once said, "He who has a why to live can bear almost any how."
-
Print a Pattern:
Write a C++ program to print the following pattern using escape sequences:************ ********** ***** **
-
Create a CV:
Make a CV that includes your Name, Father’s Name, CNIC, Qualification, Semester, CGPA, etc. Print each line separately using\n
andendl
. Use comments in your code. -
Print a Diamond:
Print a diamond shape using escape sequences\n
and\t
in a singlecout
statement. -
Square and Cube Table:
Write a program that displays the square and cube of a number in table form. Use escape sequences to format the output. -
Arithmetic Operations:
Write a C++ program that takes two numbers and displays their addition, subtraction, division, multiplication, and squares on the console. -
Average Age Calculation:
Calculate the average age of a class of five students. Prompt the user to enter the age of each student. -
Circle Calculations:
Write a C++ program that takes the radius of a circle as input from the user and outputs the circumference and area. Ensure the output is clear and readable. Add proper comments to the code and set the value of π up to 3 decimal places. -
Temperature Conversion:
Write a C++ program that takes temperature in Fahrenheit from the user and converts it to Celsius and Kelvin using the formulas:- C = (F – 32) / 1.8
- K = C + 273
-
Simple Interest Calculation:
Write a program to calculate simple interest for given values: ( P = 4000 ), ( T = 2 ), ( R = 5.5 ). Use the formula I = PTR / 100. -
Size of Data Types:
Write a C++ program to find the size of various data types:int
,short int
,float
,double
,bool
, andchar
. Use thesizeof
operator. -
Arithmetic Assignment Operations:
Take two integers as input from the user, apply arithmetic assignment operations on them, and print the results on the screen. -
Polynomial Calculation:
Write a C++ program that takes values ofa
andb
from the user and displays the result of the polynomial ( a^2 + 2ab + b^2 ). -
Complex Number Addition:
Write a C++ program that takes two complex numbers from the user and adds them. Print the resultant complex number. -
Forward and Reversed Order:
Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order. -
Integer and Character Display:
Write a C++ program that takes an integer value and a character from the user and displays both on the console window. -
Sphere Calculations:
Write a program that takes the radius of a sphere as input from the user and outputs:- The diameter of the sphere
- The surface area of the sphere
- The volume of the sphere
-
Time Conversion:
Write a C++ program that takes seconds from the user and displays the time in hours, minutes, and seconds format. For example, if the user enters 3700, the output should be "1 hour 1 minute and 40 seconds." -
Variable Swapping:
Write a C++ program to swap the values of two variables both with and without using a third variable. -
String Copy:
Write a C++ program to enter a strings1
and copy it to another strings2
. -
Polynomial Evaluation:
Write a program that asks the user to enter a value forx
and then displays the value of the polynomial (2x^5 + 3x^4 - x^3 - 2x^2 + 7x - 6). To calculate (x^5), usepow(x, 5)
. Include themath.h
library forpow
. -
Quotient and Remainder:
Write a C++ program to find the quotient and remainder of two integers by taking input from the user. -
Previous Character:
Write a C++ program that takes a character and displays its previous character using the decrement operator. -
Complex Calculation:
Write a program that asks for a number input from the keyboard, adds 34 to it, divides it by 2, takes the modulus with 4, adds 4, and then multiplies the resultant value by 5. Display the result. -
Expression Calculation:
Write a program in C++ to compute the expression (25 * 3.5 - 3 * 7.5) + 3 / (40.5 - 4.5) and print the output. -
String Operations:
Write a C++ program that prompts the user to enter two strings, then displays the strings, their lengths, their concatenation, and specific characters from each string. -
Fraction Operations:
Write a C++ program that prompts the user to enter two fractions (numerator and denominator for each), then calculates and displays their sum, difference, product, and division. -
Distance Calculation:
Write a C++ program to calculate the distance between two points where (x1), (y1), (x2), and (y2) are all double values. -
Age Check (if statements only):
Write a C++ program that checks the age of Fahad and Khurram and displays the appropriate message using if statements only. -
Age Check (nested if statements only):
Write a C++ program that checks the age of Fahad and Khurram and displays the appropriate message using nested if statements only. -
Number Comparison:
Write a C++ program that takes an integer value from the user and checks if it’s greater than 10 and less than or equal to 20. Print1
if yes and print0
if no. -
String Comparison:
Write a C++ program that takes two strings as input from the user and checks if both strings are equal. -
Even or Odd:
Using if-else, determine whether a value is even or odd. -
Greatest of Three Numbers:
Write a C++ program that prompts the user to input three integer values and find the greatest value of the three (hint: use logical operators). -
Vowel or Consonant:
Write a C++ program to check whether an alphabet entered by the user is a vowel (lower and upper case) or a consonant (lower and upper case) using if-else statements (hint: use logical operators). -
Temperature State:
Write a C++ program that prompts the user to input temperature in centigrade and display a message according to the temperature state below:- Temp < 0: Freezing weather
- Temp 0-12: Very Cold weather
- Temp 12-22: Cold weather
- Temp 22-32: Normal Temperature
- Temp 32-42: Hot
- Temp >= 42: Very Hot
-
Grade Calculation:
Write a C++ program that takes quiz marks, midterm marks, and final term marks from the user and determines the student’s grade based on the following rules:- If the average mark is greater than or equal to 95: grade A+
- If the average mark is between 85 and 95: grade A
- If the average mark is between 75 and 85: grade B
- If the average mark is between 55 and 75: grade C
- If the average mark is below 55: grade F
-
Maximum Number (Ternary Operator):
Write a C++ program that prompts the user to input two numbers and then determines the largest number using the ternary operator. -
String Equality Check (Ternary Operator):
Write a C++ program that takes two strings as input from the user and checks if both strings are equal or not using the ternary operator. -
Repeated Number Entry:
Write a C++ program that prompts the user to enter a number repeatedly until a non-positive number is entered. -
Simple Calculator:
Write a C++ program to make a simple calculator using a switch case to perform addition, subtraction, multiplication, or division. -
Sum of Numbers (Using For Loop):
Write a C++ program to calculate the sum of up to 10 numbers using a for loop. If the user enters a negative number, the loop terminates. -
Sum of Numbers (Using For Loop, Skip Negatives):
Write a C++ program to calculate the sum of up to 10 numbers using a for loop. If the user enters a negative number, it should not be added to the result. -
Area of Circle Function:
Write a function that calculates and returns the area of a circle. The radius should be a parameter of your function. Use appropriate data types. -
Maximum of Two Numbers Function:
Write a function that takes two parameters,x
andy
, and returns the maximum of the two numbers. -
Even Numbers Between Two Points:
Write a C++ program that takes two integers,i
(starting point) andn
(ending point), from the user and prints all even numbers betweeni
andn
using a while loop. For example, ifi = 5
andn = 20
, the output should be:6 8 10 12 14 16 18 20
. -
Sum of Odd Natural Numbers:
Write a C++ program to calculate the sum of odd natural numbers (1 + 3 + 5 + 7 + … + n) using a while loop. Taken
as input from the user. -
Pyramid Pattern of Asterisks:
Write a C++ program that prints a pyramid pattern of asterisks. The pattern should have 5 rows, with the number of asterisks in each row increasing by 2 for each subsequent row, starting with 1 asterisk in the first row:* *** ***** ******* *********
-
Reverse Whole Numbers:
Write a C++ program to print whole numbers in reverse fromn
to0
using a while loop.
Write a C++ program to display the days of two weeks using a nested for loop. The outer loop should represent the weeks (2 weeks), and the inner loop should represent the days (4 days per week).
Write a C++ program that takes an integer input from the user and checks if the number is even. If it is even, print "The number is even"
.
Write a C++ program that takes a character input from the user and checks if it is an uppercase letter. If it is, print "The character is uppercase"
.
Write a C++ program that takes an integer input from the user and checks if the number is divisible by 5. If it is divisible, print "The number is divisible by 5"
.
Write a C++ program that takes a character input from the user and checks if it is a vowel or consonant. Print "Vowel"
if it is a vowel; otherwise, print "Consonant"
.
Write a C++ program that takes the age of a person as input and categorizes them into the following categories:
- Child (age < 13)
- Teenager (13 <= age < 20)
- Adult (20 <= age < 60)
- Senior (age >= 60)
Write a C++ program that takes three float numbers as input and prints which number is the largest, the second largest, and the smallest.
Write a C++ program that takes an integer input from the user and prints whether the number is positive, negative, or zero using nested if statements.
Write a C++ program that takes the scores of three subjects from the user and determines if the student has passed. A student passes if all three scores are greater than or equal to 50.
Write a C++ program that takes a year as input from the user and checks if it is a leap year using nested if statements.
Write a C++ program that prints the first 10 natural numbers using a for loop.
Write a C++ program that calculates and prints the sum of all odd numbers from 1 to 100 using a for loop.
Write a C++ program that prints the factorial of a given number using a for loop.
Write a C++ program that prints a right-angled triangle pattern of stars with a height of 5 using nested for loops.
Write a C++ program that prints a 5x5 matrix filled with numbers from 1 to 25 using nested for loops.
Write a C++ program that prints the transpose of a 3x3 matrix entered by the user using nested for loops.
Write a C++ program using a while loop to calculate the sum of the first 20 positive integers.
Write a C++ program that calculates the factorial of a given number using a while loop.
Write a C++ program using a while loop to count the number of digits in a given integer.
Write a C++ program that reverses the digits of an integer using a while loop.
Write a C++ program using nested while loops to print the multiplication table from 1 to 10.
Write a C++ program using nested while loops to print a matrix of the first 5 prime numbers in each row.
Write a C++ program using nested while loops to print the following pattern:
*
**
***
****
*****
Write a C++ program using nested while loops to calculate the sum of all elements in a 3x3 matrix.
Write a C++ program that reads integers from the user in a while loop and terminates the loop when the user enters a negative number. Print the sum of all entered positive numbers before the user enters a negative number.
Write a C++ program that displays a menu to the user in a while loop. Allow the user to choose options and use the break statement to exit the menu when they select the option to quit.
Write a C++ program where the user has to guess a randomly generated number between 1 and 100. Use a while loop to allow multiple attempts and use the break statement to exit the loop when the correct number is guessed.
Write a C++ program that continually asks the user to enter numbers and adds them together. Use a break statement to exit the loop and print the total sum when the user enters zero.
Write a C++ program using nested while loops to search for a specific value in a 4x4 matrix. Use a break statement to exit the inner loop when the value is found, and print the position of the value.
Write a C++ program using nested while loops to print the following pattern, but break the inner loop when the number exceeds 5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Write a C++ program using nested while loops to calculate the sum of the diagonal elements of a 5x5 matrix. Use a break statement to exit the inner loop once the diagonal element is processed.
Write a C++ program using nested while loops to print a grid of characters. Use a break statement in the inner loop to stop printing characters in a row after printing 'z':
a b c ... z
a b c ... z
a b c ... z
Write a C++ program using nested while loops to print the following pattern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
Write a C++ program that performs basic arithmetic operations (addition, subtraction, multiplication, and division) on two user-provided numbers. The program should repeatedly prompt the user to select an operation and input two numbers, then display the result. The program should also handle invalid inputs gracefully and allow the user to perform multiple operations until they choose to exit.
Write a C++ program to declare an array of 10 integers, initialize it with values from 1 to 10, and print all the elements of the array.
Write a C++ program that reads 5 integers from the user into an array and finds the maximum element in the array.
Write a C++ program that reads 10 integers from the user into an array and calculates the sum of all the elements in the array.
Write a C++ program to reverse the elements of an array of 10 integers. Display the original and the reversed array.
Write a C++ program to sort an array of 10 integers in ascending order.
Write a C++ program to remove a specific element from an array of 10 integers. The program should shift the remaining elements to fill the gap and reduce the size of the array by one.
Write a C++ program to find and print duplicate elements in an array of 10 integers.
Write a C++ program to find the second largest element in an array of 10 integers.
Write a C++ program that tells the user if the number entered is less than or greater than 10.
Write a C++ program that tells the user if the number entered is even or odd.
Write a menu-driven C++ program that asks the user to choose the type of output they want:
- Convert the entered Celsius temperature into Fahrenheit or Kelvin.
Write a program to compare the following strings with each other and display which string is smaller:
string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; string str5 = "Big";
Write a C++ program to mark the user-entered numbers according to your grading system (Grade A, B, C, D, F). Implement all conditions and display results.
Declare and initialize the following variables and write a program to display them:
bool found = true;
bool flag = false;
int num = 1;
double x = 3.4;
int a = 5, b = 8;
char ch = 'Z';
Write a C++ program to check whether a character is an alphabet or not.
Write a C++ program to input the angles of a triangle and check whether the triangle is valid or not.
Write a C++ program to check whether a character is a vowel or a consonant.
Write a menu-driven C++ program for a simple calculator using if-else.
Write a program to input three numbers and find the maximum between all.
Write a C++ program that tells the user if the number entered is less than, greater than, or equal to 10.
Write a C++ program that tells the user if the number entered is even or odd.
Write a menu-driven C++ program that asks the user to choose the type of output they want:
- Convert the entered Celsius temperature into Fahrenheit or Kelvin.
According to your grading system, mark the user-entered percentage as Grade A, B, C, D, or F:
- Percentage >= 90: A grade
- Percentage >= 80: B grade
- Percentage >= 70: C grade
- Percentage >= 60: D grade
- Percentage >= 40: E grade
- Percentage < 40: F grade
Write a C++ program to input an upper bound from the user and print all numbers up to that limit.
Write a C++ program to input an upper bound and separate even and odd numbers up to that limit.
Write a C++ program that prints the complete alphabet using a for loop (a-z).
Write a C++ program that asks the user for a number and prints the table of that number up to 10.
Write a C++ program that displays the product of all odd numbers from 1 to 10 using a for loop.
Write a C++ program that finds the sum of squares of integers from 1 to n, where n is a positive value entered by the user (i.e., 1² + 2² + 3² + ... + n²).
Write a C++ program that counts the number of digits in the number entered by the user.
Write a C++ program to reverse a number entered by the user using a loop.
Write a C++ program to find the first and last digit of a number.
Write a C++ program to calculate the product of digits of a number.
Write a C++ program to find the frequency of each digit in a given integer.
Write a C++ program to check whether a number is an Armstrong number or not.
Write a C++ program to convert a binary number to the decimal number system.
Write a program to print Pascal's triangle up to n rows.
Read the entries of an array of 10 integers from the user. Compute x
as the average of the 10 entries, then display those entries that are greater than or equal to x
. Finally, print the calculated average.
Write a C++ program to find the minimum and maximum distance between two numbers in an array.
Take input of 10 numbers from the user, then sort and print them in both ascending and descending order.
Take an array of 5 numbers from the user and print them in reverse order.
Write a C++ program that prompts the user to enter 10 float numbers. The program should determine and display the second greatest number among the entered numbers. If there is no distinct second greatest number (i.e., all numbers are the same or there is only one unique number), inform the user accordingly.
Take 10 numbers from the user, find the smallest number in the array, and replace it with the greatest number. Then print the updated array.
Take 10 numbers from the user, then display the most frequently occurring element along with its frequency.
Write a C++ program to generate the sum of the left diagonal of a matrix.
Write a C++ program to find duplicate values in a 2D array.
Write a C++ program to move all negative elements of an array of integers to the end of the array without changing the order of the positive and negative elements.
Write a C++ program to store the temperature of two different cities for a week and display it. Also, find which city had the hottest temperature during the week.
Write a C++ program to generate the transpose of a 3x3 matrix.
Write a C++ program that initializes an array with the names of 30 students. The program should iterate through the array and print each student's name along with their corresponding roll number (starting from 1).
The output should be in the format: "Student name is [Name], roll no is [Roll Number]."
- Basic syntax and structure
- Variables, data types, and operators
- Input/output operations (using
cin
andcout
) - Control flow statements (if-else, switch-case, for loops, while loops)
- Functions
- Escape sequences (
\n
,\t
,\"
) - Comments
- Arithmetic operations
- Conditional statements
- Loops
- Arrays
- Functions
- Strings
- Matrices
- Pattern printing
- Simple calculations (area, circumference, temperature conversion, etc.)
- Data manipulation and analysis (arrays, matrices)
- Pattern generation
- Problem-solving (e.g., number guessing game, menu-driven programs)
- The exercises progressively increase in difficulty, starting with basic concepts and moving towards more complex problems.
- Some exercises involve multiple topics or require specific techniques (e.g., nested loops, arrays, functions).
- The exercises are designed to reinforce your understanding of C++ programming and provide practical experience in solving various problems.
To utilize the programs in this repository:
-
Clone this repository:
git clone https://github.com/faizan-ahmad5/100-plus-cpp-programs.git
-
Navigate to the directory containing the cloned repository:
cd 100-plus-cpp-programs
-
Compile and run any of the C++ programs using a C++ compiler (e.g., g++):
g++ program_name.cpp -o program_name ./program_name
Contributions are highly encouraged! If you'd like to add or enhance any programs, please submit a pull request. When contributing, ensure that:
- The code is clean and readable.
- Commit messages are meaningful.
- The concept being demonstrated is explained in comments.
For any questions or suggestions, feel free to reach out:
LinkedIn: https://www.linkedin.com/in/faizan-ahmad-khan5/
Email: fa3n20004@gmail.com