# C++ For Loop: Exercises, Practice, Solution

## C++ For Loop [87 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

**1.** Write a program in C++ to find the first 10 natural numbers. Go to the editor

Sample output:

The natural numbers are:

1 2 3 4 5 6 7 8 9 10

Click me to see the sample solution

**2.** Write a program in C++ to find the sum of first 10 natural numbers. Go to the editor

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

Click me to see the sample solution

**3.** Write a program in C++ to display n terms of natural number and their sum. Go to the editor

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

Click me to see the sample solution

**4.** Write a program in C++ to find the perfect numbers between 1 and 500. Go to the editor

The perfect numbers between 1 to 500 are:

6

28

496

Click me to see the sample solution

**5.** Write a program in C++ to check whether a number is prime or not. Go to the editor

Sample Output:

Input a number to check prime or not: 13

The entered number is a prime number.

Click me to see the sample solution

**6.** Write a program in C++ to find prime number within a range. Go to the editor

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

Click me to see the sample solution

**7.** Write a program in C++ to find the factorial of a number. Go to the editor

Sample output:

Input a number to find the factorial: 5

The factorial of the given number is: 120

Click me to see the sample solution

**8. ** Write a program in C++ to find the last prime number occur before the entered number. Go to the editor

Sample Output:

Input a number to find the last prime number occurs before the number: 50

47 is the last prime number before 50

Click me to see the sample solution

**9. ** Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers. Go to the editor

Sample Output:

Input the first number: 25

Input the second number: 15

The Greatest Common Divisor is: 5

Click me to see the sample solution

**10. ** Write a program in C++ to find the sum of digits of a given number. Go to the editor

Sample Output:

Input a number: 1234

The sum of digits of 1234 is: 10

Click me to see the sample solution

**11. ** Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n. Go to the editor

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

Click me to see the sample solution

**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). Go to the editor

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

Click me to see the sample solution

**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). Go to the editor

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

Click me to see the sample solution

**14. ** Write a program in C++ to find the sum of series 1 - X^2/2! + X^4/4!-.... upto nth term. Go to the editor

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

Click me to see the sample solution

**15.** Write a program in C++ to asked user to input positive integers to process count, maximum, minimum,
and average or terminate the process with -1. Go to the editor

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

Click me to see the sample solution

**16.** Write a program in C++ to list non-prime numbers from 1 to an upperbound. Go to the editor

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

Click me to see the sample solution

**17.** Write a program in C++ to print a square pattern with # character. Go to the editor

Sample Output:

Print a pattern like square with # character:

--------------------------------------------------

Input the number of characters for a side: 4

# # # #

# # # #

# # # #

# # # #

Click me to see the sample solution

**18.** Write a program in C++ to display the cube of the number upto given an integer. Go to the editor

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

Click me to see the sample solution

**19.** Write a program in C++ to display the multiplication table vertically from 1 to n. Go to the editor

Sample Output:

Input the number upto: 5

Multiplication table from 1 to 5

1x1=1 2x1=2 3x1=3 4x1=4 5x1=5

1x2=2 2x2=4 3x2=6 4x2=8 5x2=10

1x3=3 2x3=6 3x3=9 4x3=12 5x3=15

1x4=4 2x4=8 3x4=12 4x4=16 5x4=20

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45

1x10=10 2x10=20 3x10=30 4x10=40 5x10=50

Click me to see the sample solution

**20.** Write a program in C++ to display the n terms of odd natural number and their sum. Go to the editor

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

Click me to see the sample solution

**21.** Write a program in C++ to display the n terms of even natural number and their sum. Go to the editor

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

Click me to see the sample solution

**22.** Write a program in C++ to display the n terms of harmonic series and their sum. Go to the editor

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

Click me to see the sample solution

**23.** Write a program in C++ to display the sum of the series [ 9 + 99 + 999 + 9999 ...]. Go to the editor

Sample Output:

Input number of terms: 5

9 99 999 9999 99999

The sum of the sarise = 111105

Click me to see the sample solution

**24.** Write a program in C++ to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....]. Go to the editor

Sample Output:

Input the value of x: 3

Input number of terms: 5

The sum is : 16.375

Click me to see the sample solution

**25.** Write a program in C++ to find the sum of the series [ x - x^3 + x^5 + ......]. Go to the editor

Sample Output:

Input the value of x: 2

Input number of terms: 5

The values of series:

2

-8

32

-128

512

The sum of the series upto 5 term is: 410

Click me to see the sample solution

**26.** Write a program in C++ to find the sum of the series 1 +11 + 111 + 1111 + .. n terms. Go to the editor

Sample Output:

Input number of terms: 5

1 + 11 + 111 + 1111 + 11111

The sum of the series is: 12345

Click me to see the sample solution

**27.** Write a program in C++ to display the first n terms of Fibonacci series. Go to the editor

Sample Output:

Input number of terms to display: 10

Here is the Fibonacci series upto to 10 terms:

0 1 1 2 3 5 8 13 21 34

Click me to see the sample solution

**28.** Write a program in C++ to find the number and sum of all integer between 100 and 200 which are divisible by 9. Go to the editor

Sample Output:

Numbers between 100 and 200, divisible by 9:

108 117 126 135 144 153 162 171 180 189 198

The sum : 1683

Click me to see the sample solution

**29.** Write a program in C++ to find LCM of any two numbers using HCF. Go to the editor

Sample Output:

Input 1st number for LCM: 15

Input 2nd number for LCM: 25

The LCM of 15 and 25 is: 75

Click me to see the sample solution

**30.** Write a program in C++ to display the number in reverse order. Go to the editor

Sample Output:

Input a number: 12345

The number in reverse order is : 54321

Click me to see the sample solution

**31.** Write a program in C++ to find out the sum of an A.P. series. Go to the editor

Sample Output:

Input the starting number of the A.P. series: 1

Input the number of items for the A.P. series: 8

Input the common difference of A.P. series: 5

The Sum of the A.P. series are :

1 + 6 + 11 + 16 + 21 + 26 + 31 + 36 = 148

Click me to see the sample solution

**32.** Write a program in C++ to find the Sum of GP series. Go to the editor

Sample Output:

Input the starting number of the G.P. series: 3

Input the number of items for the G.P. series: 5

Input the common ratio of G.P. series: 2

The numbers for the G.P. series:

3 6 12 24 48

The Sum of the G.P. series: 93

Click me to see the sample solution

**33.** Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers. Go to the editor

Sample Output:

Input a positive integer: 20

20 = 3 + 17

20 = 7 + 13

Click me to see the sample solution

**34.** Write a program in C++ to find the length of a string without using the library function. Go to the editor

Sample Output:

Input a string: w3resource.com

The string contains 14 number of characters.

So, the length of the string w3resource.com is:14

Click me to see the sample solution

**35.** Write a program in C++ to display the pattern like right angle triangle using an asterisk. Go to the editor

Sample Output:

Input number of rows: 5

*

**

***

****

*****

Click me to see the sample solution

**36.** Write a program in C++ to display the pattern like right angle triangle with number. Go to the editor

Sample Output:

Input number of rows: 5

1

12

123

1234

12345

Click me to see the sample solution

**37.** Write a program in C++ to make such a pattern like right angle triangle using number which will repeat the number for that row. Go to the editor

Sample Output:

Input number of rows: 5 1 22 333 4444 55555Click me to see the sample solution

**38.** Write a program in C++ to make such a pattern like right angle triangle with number increased by 1. Go to the editor

Sample Output:

Input number of rows: 4 1 2 3 4 5 6 7 8 9 10Click me to see the sample solution

**39.** Write a program in C++ to make such a pattern like a pyramid with numbers increased by 1. Go to the editor

Sample Output:

Input number of rows: 4 1 2 3 4 5 6 7 8 9 10

Click me to see the sample solution

**40.** Write a program in C++ to make such a pattern like a pyramid with an asterisk. Go to the editor

Sample Output:

Input number of rows: 5 * * * * * * * * * * * * * * *Click me to see the sample solution

**41.** Write a program in C++ to make such a pattern like a pyramid using number and a number will repeat for a row. Go to the editor

Sample Output:

Input number of rows: 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5Click me to see the sample solution

**42.** Write a program in C++ to display the pattern like a pyramid using asterisk and each row contain an odd number of asterisks. Go to the editor

Sample Output:

Input number of rows: 5 * *** ***** *******Click me to see the sample solution

**43.** Write a program in C++ to print the Floyd's Triangle. Go to the editor

Sample Output:

Input number of rows: 5 1 01 101 0101 10101Click me to see the sample solution

**44.** Write a program in C++ to display the pattern like a diamond. Go to the editor

Sample Output:

Input number of rows (half of the diamond): 5 * *** ***** ******* ********* ******* ***** *** *Click me to see the sample solution

**45.** Write a program in C++ to display Pascal's triangle like pyramid. Go to the editor

Sample Output:

Input number of rows: 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1Click me to see the sample solution

**46.** Write a program in C++ to display Pascal's triangle like right angle traingle. Go to the editor

Sample Output:

Input number of rows: 7 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1Click me to see the sample solution

**47.** Write a program in C++ to display such a pattern for n number of rows using number. Each row will contain odd numbers of number. The first and last number of each row will be 1 and middle column will be the row number. Go to the editor

Sample Output:

Input number of rows: 5 1 121 12321 1234321 123454321Click me to see the sample solution

**48.** Write a program in C++ to display the pattern like pyramid using the alphabet. Go to the editor

Sample Output:

Input the number of Letters (less than 26) in the Pyramid: 5 A A B A A B C B A A B C D C B A A B C D E D C B AClick me to see the sample solution

**49.** Write a program in C++ to print a pyramid of digits as shown below for n number of lines. Go to the editor

1 232 34543 4567654 567898765Sample Output:

Input the number of rows: 5 1 232 34543 4567654 567898765Click me to see the sample solution

**50.** Write a program in C++ to print a pattern like highest numbers of columns appear in first row. Go to the editor

Sample Output:

Input the number of rows: 5 12345 2345 345 45 5Click me to see the sample solution

**51.** Write a program in C++ to display the pattern using digits with right justified and the highest columns appears in first row. Go to the editor

Sample Output:

Input number of rows: 5 12345 1234 123 12 1Click me to see the sample solution

**52.** Write a program in C++ to display the pattern using digits with left justified and the highest columns appears in first row in descending order. Go to the editor

Sample Output:

Input number of rows: 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1Click me to see the sample solution

**53.** Write a program in C++ to display the pattern like right angle triangle with right justified using digits. Go to the editor

Sample Output:

Input number of rows: 5 1 21 321 4321 54321Click me to see the sample solution

**54.** Write a program in C++ to display the pattern power of 2, triangle. Go to the editor

Sample Output:

Display the pattern like pyramid with power of 2: ------------------------------------------------------ Input the number of rows: 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1Click me to see the sample solution

**55.** Write a program in C++ to display such a pattern for n number of rows using number. Each row will contain odd numbers of number. The first and last number of each row will be 1 and middle column will be the row number. n numbers of columns will appear in 1st row. Go to the editor

Sample Output:

Input number of rows: 7 1234567654321 12345654321 123454321 1234321 12321 121 1Click me to see the sample solution

**56.** Write a program in C++ to find the first and last digit of a number. Go to the editor

Sample Output:

Input any number: 5679

The first digit of 5679 is: 5

The last digit of 5679 is: 9

Click me to see the sample solution

**57.** Write a program in C++ to find the sum of first and last digit of a number. Go to the editor

Sample Output:

Input any number: 12345

The first digit of 12345 is: 1

The last digit of 12345 is: 5

The sum of first and last digit of 12345 is: 6

Click me to see the sample solution

**58.** Write a program in C++ to calculate product of digits of any number. Go to the editor

Sample Output:

Input a number: 3456

The product of digits of 3456 is: 360

Click me to see the sample solution

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

Sample Output:

Input any number: 122345

The frequency of 0 = 0

The frequency of 1 = 1

The frequency of 2 = 2

The frequency of 3 = 1

The frequency of 4 = 1

The frequency of 5 = 1

The frequency of 6 = 0

The frequency of 7 = 0

The frequency of 8 = 0

The frequency of 9 = 0

Click me to see the sample solution

**60.** Write a program in C++ to input any number and print it in words. Go to the editor

Sample Output:

Input any number: 8309

Eight Three Zero Nine

Click me to see the sample solution

**61.** Write a program in C++ to print all ASCII character with their values. Go to the editor

Sample Output:

Input the starting value for ASCII characters: 65

Input the ending value for ASCII characters: 75

The ASCII characters:

65 --> A

66 --> B

67 --> C

68 --> D

69 --> E

70 --> F

71 --> G

72 --> H

73 --> I

74 --> J

75 --> K

Click me to see the sample solution

**62.** Write a program in C++ to find power of any number using for loop. Go to the editor

Sample Output:

Input the base: 2

Input the exponent: 5

2 ^ 5 = 32

Click me to see the sample solution

**63.** Write a program in C++ to enter any number and print all factors of the number. Go to the editor

Sample Output:

Input a number: 63

The factors are: 1 3 7 9 21 63

Click me to see the sample solution

**64.** Write a program in C++ to find one's complement of a binary number. Go to the editor

Sample Output:

Input a 8 bit binary value: 10100101

The original binary = 10100101

After ones complement the number = 01011010

Click me to see the sample solution

**65.** Write a program in C++ to find two's complement of a binary number. Go to the editor

Sample Output:

Input a 8 bit binary value: 01101110

The original binary = 01101110

After ones complement the value = 10010001

After twos complement the value = 10010010

Click me to see the sample solution

**66.** Write code to create a checkerboard pattern with the words "black" and "white". Go to the editor

Sample Output:

Input number of rows: 5

black-white-black-white-black

white-black-white-black-white

black-white-black-white-black

white-black-white-black-white

black-white-black-white-black

Click me to see the sample solution

**67.** Write a program in C++ to calculate the sum of the series 1?2+2?3+3?4+4.5+5.6+....... Go to the editor

Sample Output:

Input the last integer between 1 to 98 without fraction you want to add: 10
1.2 + 2.3 + 3.4 + 4.5 + 5.6 + 6.7 + 7.8 + 8.9 + 9.1 + 10.11
The sum of the series =59.61

Click me to see the sample solution

**68.** Write a program that will print the first N numbers for a specific base. Go to the editor

Sample Output:

Print the first N numbers for a specific base:

The number 11 in base 10 = 1*(10^1)+1*(10^0)=11

Similarly the number 11 in base 7 = 1*(7^1)+1*(7^0)=8

----------------------------------------------------------------

Input the number of term: 15

Input the base: 9

The numbers in base 9 are:

1 2 3 4 5 6 7 8 10 11 12 13 14 15 16

Click me to see the sample solution

**69.** Write a program in C++ to produce a square matrix with 0's down the main diagonal, 1's in the
entries just above and below the main diagonal, 2's above and below that, etc. Go to the editor

0 1 2 3 4

1 0 1 2 3

2 1 0 1 2

3 2 1 0 1

4 3 2 1 0

Sample Output:

Input number or rows: 8 0 1 2 3 4 5 6 7 1 0 1 2 3 4 5 6 2 1 0 1 2 3 4 5 3 2 1 0 1 2 3 4 4 3 2 1 0 1 2 3 5 4 3 2 1 0 1 2 6 5 4 3 2 1 0 1 7 6 5 4 3 2 1 0Click me to see the sample solution

**70.** Write a program in C++ to convert a decimal number to binary number. Go to the editor

Sample Output:

Input a decimal number: 35

The binary number is: 100011

Click me to see the sample solution

**71.** Write a program in C++ to convert a decimal number to hexadecimal number. Go to the editor

Sample Output:

Input a decimal number: 43

The hexadecimal number is : 2B

Click me to see the sample solution

**72.** Write a program in C++ to convert a decimal number to octal number. Go to the editor

Sample Output:

Input a decimal number: 15

The octal number is: 17

Click me to see the sample solution

**73.** Write a program in C++ to convert a binary number to decimal number. Go to the editor

Sample Output:

Input a binary number: 1011

The decimal number: 11

Click me to see the sample solution

**74.** Write a program in C++ to convert a binary number to hexadecimal number. Go to the editor

Sample Output:

Input a binary number: 1011

The hexadecimal value: B

Click me to see the sample solution

**75.** Write a program in C++ to convert a binary number to octal number. Go to the editor

Sample Output:

Input a binary number: 1011

The equivalent octal value of 1011 is : 13

Click me to see the sample solution

**76.** Write a program in C++ to convert a octal number to decimal number. Go to the editor

Sample Output:

Input any octal number: 17

The equivalent decimal number: 15

Click me to see the sample solution

**77.** Write a program in C++ to convert a octal number to binary number. Go to the editor

Sample Output:

Input any octal number: 17

The equivalent binary number: 1111

Click me to see the sample solution

**78.** Write a program in C++ to convert a octal number to a hexadecimal number. Go to the editor

Sample Output:

Input any octal number: 77

The hexadecimal value of 77 is: 3F

Click me to see the sample solution

**79.** Write a program in C++ to convert a hexadecimal number to decimal number. Go to the editor

Sample Output:

Input any 32-bit Hexadecimal Number: 25

The value in decimal number is: 37

Click me to see the sample solution

**80.** Write a program in C++ to convert hexadecimal number to binary number. Go to the editor

Sample Output:

Input any 32-bit Hexadecimal Number: 5f

The equivalant binary number is: 1011111

Click me to see the sample solution

**81.** Write a program in C++ to convert a hexadecimal number to octal number. Go to the editor

Sample Output:

Input any 32-bit Hexadecimal Number: 5f
The equivalant octal number is: 137

Click me to see the sample solution

**82.** Write a program in C++ to compare two numbers. Go to the editor

Sample Output:

Input the first integer: 25

Input the second integer: 15

25 != 15

25 > 15

25 >= 15

Click me to see the sample solution

**83.** Write a program in C++ to compute the sum of the digits of an integer. Go to the editor

Sample Output:

Input any number: 25

The sum of the digits of the number 25 is: 7

Click me to see the sample solution

**84.** Write a program in C++ to compute the sum of the digits of an integer using function. Go to the editor

Sample Output:

Input any number: 255
The sum of the digits of the number 255 is: 12

Click me to see the sample solution

**85.** Write a program in C++ to reverse a string. Go to the editor

Sample Output:

Enter a string: w3resource
The string in reverse are: ecruoser3w

Click me to see the sample solution

**86.** Write a program in C++ to count the letters, spaces, numbers and other characters of an input string. Go to the editor

Sample Output:

Enter a string: This is w3resource.com

The number of characters in the string is: 22

The number of alphabets are: 18

The number of digits are: 1

The number of spaces are: 2

The number of other characters are: 1

Click me to see the sample solution

**87.** Write a program in C++ to create and display unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there. Go to the editor

Sample Output:

The three-digit numbers are:

123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432

Total number of the three-digit-number is: 24

Click me to see the sample solution

**CPP Code Editor:**

**More to Come !**

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

**New Content published on w3resource:**- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework