C++ Exercises Topic-wise for beginner to become expert programmer
C++ Exercises
Basic Exercise
Operator Exercise
if…else Exercise
switch case Exercise
For loop Exrecise
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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 # # # # # # # # # # # # # # # #
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
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
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
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
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