Skip to content

Latest commit

 

History

History
561 lines (405 loc) · 26.3 KB

README.md

File metadata and controls

561 lines (405 loc) · 26.3 KB

100-plus-cpp-programs

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.

📌 Tasks

  1. 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.

  2. 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."

  3. Print a Pattern:
    Write a C++ program to print the following pattern using escape sequences:

    ************
    **********
    *****
    ** 
    
  4. Create a CV:
    Make a CV that includes your Name, Father’s Name, CNIC, Qualification, Semester, CGPA, etc. Print each line separately using \n and endl. Use comments in your code.

  5. Print a Diamond:
    Print a diamond shape using escape sequences \n and \t in a single cout statement.

  6. 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.

  7. Arithmetic Operations:
    Write a C++ program that takes two numbers and displays their addition, subtraction, division, multiplication, and squares on the console.

  8. Average Age Calculation:
    Calculate the average age of a class of five students. Prompt the user to enter the age of each student.

  9. 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.

  10. 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
  11. 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.

  12. Size of Data Types:
    Write a C++ program to find the size of various data types: int, short int, float, double, bool, and char. Use the sizeof operator.

  13. Arithmetic Assignment Operations:
    Take two integers as input from the user, apply arithmetic assignment operations on them, and print the results on the screen.

  14. Polynomial Calculation:
    Write a C++ program that takes values of a and b from the user and displays the result of the polynomial ( a^2 + 2ab + b^2 ).

  15. Complex Number Addition:
    Write a C++ program that takes two complex numbers from the user and adds them. Print the resultant complex number.

  16. 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.

  17. 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.

  18. 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
  19. 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."

  20. Variable Swapping:
    Write a C++ program to swap the values of two variables both with and without using a third variable.

  21. String Copy:
    Write a C++ program to enter a string s1 and copy it to another string s2.

  22. Polynomial Evaluation:
    Write a program that asks the user to enter a value for x and then displays the value of the polynomial (2x^5 + 3x^4 - x^3 - 2x^2 + 7x - 6). To calculate (x^5), use pow(x, 5). Include the math.h library for pow.

  23. Quotient and Remainder:
    Write a C++ program to find the quotient and remainder of two integers by taking input from the user.

  24. Previous Character:
    Write a C++ program that takes a character and displays its previous character using the decrement operator.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. Distance Calculation:
    Write a C++ program to calculate the distance between two points where (x1), (y1), (x2), and (y2) are all double values.

  30. 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.

  31. 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.

  32. 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. Print 1 if yes and print 0 if no.

  33. String Comparison:
    Write a C++ program that takes two strings as input from the user and checks if both strings are equal.

  34. Even or Odd:
    Using if-else, determine whether a value is even or odd.

  35. 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).

  36. 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).

  37. 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
  38. 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
  39. 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.

  40. 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.

  41. Repeated Number Entry:
    Write a C++ program that prompts the user to enter a number repeatedly until a non-positive number is entered.

  42. Simple Calculator:
    Write a C++ program to make a simple calculator using a switch case to perform addition, subtraction, multiplication, or division.

  43. 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.

  44. 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.

  45. 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.

  46. Maximum of Two Numbers Function:
    Write a function that takes two parameters, x and y, and returns the maximum of the two numbers.

  47. Even Numbers Between Two Points:
    Write a C++ program that takes two integers, i (starting point) and n (ending point), from the user and prints all even numbers between i and n using a while loop. For example, if i = 5 and n = 20, the output should be: 6 8 10 12 14 16 18 20.

  48. 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. Take n as input from the user.

  49. 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:

    *
    ***
    *****
    *******
    *********
    
  50. Reverse Whole Numbers:
    Write a C++ program to print whole numbers in reverse from n to 0 using a while loop.

51. Display Days of Two Weeks (Nested For 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).

52. Check Even Number

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".

53. Check Uppercase Character

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".

54. Check Divisibility by 5

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".

55. Check Vowel or Consonant

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".

56. Categorize Age

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)

57. Find Largest, Second Largest, and Smallest

Write a C++ program that takes three float numbers as input and prints which number is the largest, the second largest, and the smallest.

58. Check Positive, Negative, or Zero (Nested If Statements)

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.

59. Pass or Fail

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.

60. Leap Year Check (Nested If Statements)

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.

61. Print First 10 Natural Numbers

Write a C++ program that prints the first 10 natural numbers using a for loop.

62. Sum of Odd Numbers from 1 to 100

Write a C++ program that calculates and prints the sum of all odd numbers from 1 to 100 using a for loop.

63. Factorial Using For Loop

Write a C++ program that prints the factorial of a given number using a for loop.

64. Right-Angled Triangle Pattern

Write a C++ program that prints a right-angled triangle pattern of stars with a height of 5 using nested for loops.

65. 5x5 Matrix of Numbers

Write a C++ program that prints a 5x5 matrix filled with numbers from 1 to 25 using nested for loops.

66. Transpose of a 3x3 Matrix

Write a C++ program that prints the transpose of a 3x3 matrix entered by the user using nested for loops.

67. Basic Summation

Write a C++ program using a while loop to calculate the sum of the first 20 positive integers.

68. Factorial Calculation Using While Loop

Write a C++ program that calculates the factorial of a given number using a while loop.

69. Digit Count

Write a C++ program using a while loop to count the number of digits in a given integer.

70. Reverse Digits

Write a C++ program that reverses the digits of an integer using a while loop.

71. Multiplication Table (1 to 10)

Write a C++ program using nested while loops to print the multiplication table from 1 to 10.

72. Prime Numbers Matrix

Write a C++ program using nested while loops to print a matrix of the first 5 prime numbers in each row.

73. Pattern Printing

Write a C++ program using nested while loops to print the following pattern:

*
**
***
****
*****

74. Sum of Matrix Elements

Write a C++ program using nested while loops to calculate the sum of all elements in a 3x3 matrix.

75. Early Termination

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.

76. Simple Menu with Break

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.

77. Number Guessing Game

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.

78. Sum Until Zero

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.

79. Matrix Search

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.

80. Pattern with Condition

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  

81. Sum of Diagonal Elements

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.

82. Character Grid

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  

83. Number Pattern

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  

84. Basic Arithmetic Operations

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.

85. Array Initialization and Printing

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.

86. Maximum Element in Array

Write a C++ program that reads 5 integers from the user into an array and finds the maximum element in the array.

87. Sum of Array Elements

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.

88. Reverse Array

Write a C++ program to reverse the elements of an array of 10 integers. Display the original and the reversed array.

89. Sort Array in Ascending Order

Write a C++ program to sort an array of 10 integers in ascending order.

90. Remove Element from Array

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.

91. Find and Print Duplicate Elements in an Array

Write a C++ program to find and print duplicate elements in an array of 10 integers.

92. Second Largest Element in an Array

Write a C++ program to find the second largest element in an array of 10 integers.

93. Check Number Less Than or Greater Than 10

Write a C++ program that tells the user if the number entered is less than or greater than 10.

94. Check Even or Odd Number

Write a C++ program that tells the user if the number entered is even or odd.

95. Menu-Driven Temperature Conversion

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.

96. Compare Strings

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";

97. Grading System

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.

98. Variable Declarations and Output

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';

99. Check if Character is Alphabet

Write a C++ program to check whether a character is an alphabet or not.

100. Check Valid Triangle

Write a C++ program to input the angles of a triangle and check whether the triangle is valid or not.

101. Check Vowel or Consonant

Write a C++ program to check whether a character is a vowel or a consonant.

102. Menu-Driven Simple Calculator

Write a menu-driven C++ program for a simple calculator using if-else.

103. Find Maximum of Three Numbers

Write a program to input three numbers and find the maximum between all.

104. Check Number Less Than, Greater Than, or Equal to 10

Write a C++ program that tells the user if the number entered is less than, greater than, or equal to 10.

105. Check Even or Odd Number

Write a C++ program that tells the user if the number entered is even or odd.

106. Menu-Driven Temperature Conversion

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.

107. Grading System Based on Percentage

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

108. Print Numbers up to Upper Bound

Write a C++ program to input an upper bound from the user and print all numbers up to that limit.

109. Separate Even and Odd Numbers up to Upper Bound

Write a C++ program to input an upper bound and separate even and odd numbers up to that limit.

110. Print Alphabets (a-z)

Write a C++ program that prints the complete alphabet using a for loop (a-z).

111. Print Multiplication Table

Write a C++ program that asks the user for a number and prints the table of that number up to 10.

112. Product of Odd Numbers from 1 to 10

Write a C++ program that displays the product of all odd numbers from 1 to 10 using a for loop.

113. Sum of Squares from 1 to n

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²).

114. Count Digits in a Number

Write a C++ program that counts the number of digits in the number entered by the user.

115. Reverse a Number

Write a C++ program to reverse a number entered by the user using a loop.

116. Find First and Last Digit of a Number

Write a C++ program to find the first and last digit of a number.

117. Product of Digits of a Number

Write a C++ program to calculate the product of digits of a number.

118. Frequency of Each Digit in an Integer

Write a C++ program to find the frequency of each digit in a given integer.

119. Check Armstrong Number

Write a C++ program to check whether a number is an Armstrong number or not.

120. Convert Binary to Decimal

Write a C++ program to convert a binary number to the decimal number system.

121. Pascal Triangle

Write a program to print Pascal's triangle up to n rows.

122. Filter and Average of Array

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.

123. Minimum and Maximum Distance in Array

Write a C++ program to find the minimum and maximum distance between two numbers in an array.

124. Sort Array in Ascending and Descending Order

Take input of 10 numbers from the user, then sort and print them in both ascending and descending order.

125. Reverse Array

Take an array of 5 numbers from the user and print them in reverse order.

126. Second Greatest Float Number

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.

127. Smallest to Greatest in Array

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.

128. Most Occurring Element in Array

Take 10 numbers from the user, then display the most frequently occurring element along with its frequency.

129. Sum of Left Diagonal of Matrix

Write a C++ program to generate the sum of the left diagonal of a matrix.

130. Duplicate Values in 2D Array

Write a C++ program to find duplicate values in a 2D array.

131. Move Negative Elements to End of 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.

132. Store and Display Temperature of Cities

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.

133. Transpose of 3x3 Matrix

Write a C++ program to generate the transpose of a 3x3 matrix.

134. Array of Student Names

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]."

📋Topics Covered in the Exercises

Fundamentals of C++:

  • Basic syntax and structure
  • Variables, data types, and operators
  • Input/output operations (using cin and cout)
  • Control flow statements (if-else, switch-case, for loops, while loops)
  • Functions

Specific Concepts:

  • Escape sequences (\n, \t, \")
  • Comments
  • Arithmetic operations
  • Conditional statements
  • Loops
  • Arrays
  • Functions
  • Strings
  • Matrices
  • Pattern printing

Applications:

  • 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)

Additional Notes:

  • 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.

🚀 How to Use

To utilize the programs in this repository:

  1. Clone this repository:

    git clone https://github.com/faizan-ahmad5/100-plus-cpp-programs.git
  2. Navigate to the directory containing the cloned repository:

    cd 100-plus-cpp-programs
  3. 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

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.

📬 Contact

For any questions or suggestions, feel free to reach out:

LinkedIn: https://www.linkedin.com/in/faizan-ahmad-khan5/

Email: fa3n20004@gmail.com