C++ Exercises Topic-wise for beginner to become expert programmer

Welcome to our C++ Learning Hub! 🚀
Unlock the power of C++ programming with our comprehensive resource that caters to learners of all levels. Whether you’re a beginner eager to grasp the fundamentals or an experienced coder seeking advanced challenges, we’ve got you covered.

📘 Basics to Advance:
Start your journey with our structured lessons covering the basics of C++ syntax, data types, control flow, and object-oriented programming. Move seamlessly through intermediate topics like functions, pointers, and classes. Progress to advanced concepts such as templates, polymorphism, and exception handling.

💡 Exercise Levels:
Challenge yourself with a diverse range of exercises designed for every proficiency level:

Easy Exercises: Perfect for beginners, these exercises focus on reinforcing fundamental concepts and building a strong foundation.
Medium Exercises: Ready to take it up a notch? Our medium-level exercises provide a balance of complexity, testing your understanding of key C++ concepts.
Hard Exercises: For seasoned developers and those craving a mental workout, our hard-level exercises delve into intricate scenarios, pushing your C++ skills to the limit.

🤝 Community Support:
Join our vibrant community of learners and seasoned developers. Share your insights, seek guidance, and collaborate on solving problems together. Learning C++ is more fun when you do it as part of a supportive community.

🌐 Resources Beyond Exercises:
In addition to exercises, access tutorials, coding tips, and real-world examples to enhance your understanding. Explore project ideas and implement what you’ve learned in practical scenarios.

Embark on a rewarding journey of mastering C++ with our user-friendly interface, engaging content, and a dynamic community. Start coding, and let’s build a brighter, more efficient future together! 💻🌟

  1. C++ Basic Exercise Part-1(85)
  2. C++ Basic Logic Building and Algorithm
    1. Write a C++ program to compute the sum of two given integer values. If the two values are the same, then return triple their sum.
      Sample Input
      1, 2
      3, 2
      2, 2
      Sample Output:
      3
      5
      12
    2. Write a C++ program to find the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.
      Sample Input:
      53
      30
      51
      Sample Output:
      6
      21
      0
    3. Write a C++ program to check two given integers, and return true if one of them is 30 or if their sum is 30.
      Sample Input:
      30, 0
      25, 5
      20, 30
      20, 25
      Sample Output:
      1
      1
      1
      0
    4. Write a C++ program to check a given integer and return true if it is within 10 of 100 or 200.
      Sample Input:
      103
      90
      89
      Sample Output:
      1
      1
      0
    5. Write a C++ program to create a string where ‘if’ is added to the front of a given string. If the string already begins with ‘if’, return the string unchanged.
      Sample Input:
      “if else”
      “else”
      Sample Output:
      if else
      if else
    6. Write a C++ program to remove the character at a given position in the string. The given position will be in the range 0..string length -1 inclusive.
      Sample Input:
      “Python”, 1
      “Python”, o
      “Python”, 4
      Sample Output:
      Pthon
      ython
      Pythn
    7. Write a C++ program to exchange the first and last characters in a given string and return the result string.
      Sample Input:
      “abcd”
      “a”
      “xy”
      Sample output:
      dbca
      a
      yx
    8. Write a C++ program to create a string that is 4 copies of the 2 front characters of a given string. If the given string length is less than 2 return the original string.
      Sample Input:
      “C Sharp”
      “JS”
      “a”
      Sample Output:
      C C C C
      JSJSJSJS
      a
    9. Write a C++ program to create a string with the last character added at the front and back of a given string of length 1 or more
      Sample Input:
      “Red”
      “Green”
      “1”
      Sample Output:
      dRedd
      nGreenn
      111
    10. Write a C++ program to check if a given positive number is a multiple of 3 or a multiple of 7.
      Sample Input
      3
      14
      12
      37
      Sample Output:
      1
      1
      1
      0
    11. Write a C++ program to create a string taking the first 3 characters of a given string. Then, return the string with the 3 characters added to both the front and back. If the given string length is less than 3, use whatever characters are there.
      Sample Input:
      “Python”
      “JS”
      “Code”
      Sample Output:
      PytPythonPyt
      JSJSJS
      CodCodeCod
    12. Write a C++ program to check if a given string starts with ‘C#’ or not.
      Sample Input:
      “C++ Sharp”
      “C#”
      “C++”
      Sample Output:
      1
      1
      0
    13. Write a C++ program to check if one given temperature is less than 0 and the other is greater than 100.
      Sample Input:
      120, -1
      -1, 120
      2, 120
      Sample Output:
      1
      1
      0
    14. Write a C++ program to check two given integers whether either of them is in the range 100..200 inclusive.
      Sample Input:
      100, 199
      250, 300
      105, 190
      Sample Output:
      1
      0
      1
    15. Write a C++ program to check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the range, otherwise false.
      Sample Input:
      11, 20, 12
      30, 30, 17
      25, 35, 50
      15, 12, 8
      Sample Output:
      1
      1
      1
      0
    16. Write a C++ program to check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the range, otherwise false.
      Sample Input:
      20, 84
      14, 50
      11, 45
      25, 40
      Sample Output:
      1
      1
      1
      0
    17. Write a C++ program to check if the string ‘yt’ appears at index 1 in a given string. If it appears return a string without ‘yt’ otherwise return the original string.
      Sample Input:
      “Python”
      “ytade”
      “jsues”
      Sample Output:
      Phon
      ytade
      jsues
    18. Write a C++ program to check the largest number among three given integers.
      Sample Input:
      1,2,3
      1,3,2
      1,1,1
      1,2,2
      Sample Output:
      3
      3
      1
      2
    19. Write a C++ program to check which number is closest to the value 100 among two given integers. Return 0 if the two numbers are equal.
      Sample Input:
      78, 95
      95, 95
      99, 70
      Sample Output:
      95
      0
      99
    20. Write a C++ program to check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
      Sample Input:
      78, 95
      25, 35
      40, 50
      55, 60
      Sample Output:
      0
      0
      1
      1
  3. Operator Exercise
  4. if…else Exercise
  5. switch case Exercise
  6. For loop Exrecise
    1. Write a program in C++ to find the first 10 natural numbers.
      Sample output:
      The natural numbers are:
      1 2 3 4 5 6 7 8 9 10
    2. Write a program in C++ to find the sum of the first 10 natural numbers.
      Sample Output:
      Find the first 10 natural numbers:
      —————————————
      The natural numbers are:
      1 2 3 4 5 6 7 8 9 10
      The sum of first 10 natural numbers: 55
    3. Write a program in C++ to display n terms of natural numbers and their sum.
      Sample Output:
      Input a number of terms: 7
      The natural numbers upto 7th terms are:
      1 2 3 4 5 6 7
      The sum of the natural numbers is: 28
    4. Write a program in C++ to find the perfect numbers between 1 and 500.
      Expected Output:
      The perfect numbers between 1 to 500 are:
      6
      28
      496
    5. Write a program in C++ to check whether a number is prime or not.
      Sample Output:
      Input a number to check prime or not: 13
      The entered number is a prime number.
    6. Write a program in C++ to find a prime number within a range.
      Input number for starting range: 1
      Input number for ending range: 100
      The prime numbers between 1 and 100 are:
      2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
      The total number of prime numbers between 1 to 100 is: 25
    7. Write a program in C++ to find the factorial of a number.
      Sample output:
      Input a number to find the factorial: 5
      The factorial of the given number is: 120
    8. Write a program in C++ to find the last prime number that occurs before the entered number.
      Sample Output:
      Input a number to find the last prime number occurs before the number: 50
      47 is the last prime number before 50
    9. Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers.
      Sample Output:
      Input the first number: 25
      Input the second number: 15
      The Greatest Common Divisor is: 5
    10. Write a program in C++ to find the sum of the digits of a given number.
      Sample Output:
      Input a number: 1234
      The sum of digits of 1234 is: 10
    11. Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n.
      Sample Output:
      Input the value for nth term: 5
      1/1^1 = 1
      1/2^2 = 0.25
      1/3^3 = 0.037037
      1/4^4 = 0.00390625
      1/5^5 = 0.00032
      The sum of the above series is: 1.29126
    12. Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n).
      Sample Output:
      Input the value for nth term: 5
      1*1 = 1
      2*2 = 4
      3*3 = 9
      4*4 = 16
      5*5 = 25
      The sum of the above series is: 55
    13. Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+…+n).
      Sample Output:
      Input the value for nth term: 5
      1 = 1
      1+2 = 3
      1+2+3 = 6
      1+2+3+4 = 10
      1+2+3+4+5 = 15
      The sum of the above series is: 35
    14. Write a program in C++ to find the sum of series 1 – X^2/2! + X^4/4!-…. upto nth term.
      Sample Output:
      Input the value of X: 3
      Input the value for nth term: 4
      term 1 value is: 1
      term 2 value is: -4.5
      term 3 value is: 3.375
      term 4 value is: -1.0125
      The sum of the above series is: -1.1375
    15. Write a C++ program that asks the user to enter positive integers in order to process count, maximum, minimum, and average or terminate the process with -1.
      Sample Output:
      Your input is for termination. Here is the result below:
      Number of positive integers is: 4
      The maximum value is: 9
      The minimum value is: 3
      The average is 6.00
    16. Write a C++ program to list non-prime numbers from 1 to an upperbound.
      Sample Output:
      Input the upperlimit: 25
      The non-prime numbers are:
      4 6 8 9 10 12 14 15 16 18 20 21 22 24 25
    17. Write a program in C++ to print a square pattern with the # character.
      Sample Output:
      Print a pattern like square with # character:
      ————————————————–
      Input the number of characters for a side: 4
      # # # #
      # # # #
      # # # #
      # # # #
    18. Write a program in C++ to display the cube of the number up to an integer.
      Sample Output:
      Input the number of terms : 5
      Number is : 1 and the cube of 1 is: 1
      Number is : 2 and the cube of 2 is: 8
      Number is : 3 and the cube of 3 is: 27
      Number is : 4 and the cube of 4 is: 64
      Number is : 5 and the cube of 5 is: 125
    19. Write a program in C++ to display the multiplication table vertically from 1 to n.
      Sample Output:
      Input the number upto: 5
      Multiplication table from 1 to 5
      1×1=1 2×1=2 3×1=3 4×1=4 5×1=5
      1×2=2 2×2=4 3×2=6 4×2=8 5×2=10
      1×3=3 2×3=6 3×3=9 4×3=12 5×3=15
      1×4=4 2×4=8 3×4=12 4×4=16 5×4=20
      1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
      1×6=6 2×6=12 3×6=18 4×6=24 5×6=30
      1×7=7 2×7=14 3×7=21 4×7=28 5×7=35
      1×8=8 2×8=16 3×8=24 4×8=32 5×8=40
      1×9=9 2×9=18 3×9=27 4×9=36 5×9=45
      1×10=10 2×10=20 3×10=30 4×10=40 5×10=50
    20. Write a C++ program that displays the sum of n odd natural numbers.
      Sample Output:
      Input number of terms: 5
      The odd numbers are: 1 3 5 7 9
      The Sum of odd Natural Numbers upto 5 terms: 25
    21. Write a C++ program that displays the sum of the n terms of even natural numbers.
      Sample Output:
      Input number of terms: 5
      The even numbers are: 2 4 6 8 10
      The Sum of even Natural Numbers upto 5 terms: 30
    22. Write a program in C++ to display the n terms of a harmonic series and their sum.
      1 + 1/2 + 1/3 + 1/4 + 1/5 … 1/n terms.
      Sample Output:
      Input number of terms: 5
      1/1 + 1/2 + 1/3 + 1/4 + 1/5
      The sum of the series upto 5 terms: 2.28333
  7. while Loop Exercise
  8. do … while Loop Exercise
  9. Array Exercise
  10. Date exercise
  11. Math Exercise

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment