w3resource

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

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
Click me to see the sample solution

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
Click me to see the sample solution

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
Click me to see the sample solution

4. Write a program in C++ to find the perfect numbers between 1 and 500.
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.
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 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
Click me to see the sample solution

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
Click me to see the sample solution

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
Click me to see the sample solution

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
Click me to see the sample solution

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
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.
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).
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).
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.
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 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
Click me to see the sample solution

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
Click me to see the sample solution

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
# # # #
# # # #
# # # #
# # # #
Click me to see the sample solution

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
Click me to see the sample solution

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
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 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
Click me to see the sample solution

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
Click me to see the sample solution

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
Click me to see the sample solution

23. Write a program in C++ to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
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!+....].
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 + ......].
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.
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 the Fibonacci series.
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 integers between 100 and 200 which are divisible by 9.
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 the LCM of any two numbers using HCF.
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 numbers in reverse order.
Sample Output:
Input a number: 12345
The number in reverse order is : 54321
Click me to see the sample solution

31. Write a C++ program to find the sum of an A.P. series.
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 C++ program to find the sum of the GP series.
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 expressed as the sum of two.
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.
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 a pattern like a right angle triangle using an asterisk.
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.
Sample Output:
Input number of rows: 5
1
12
123
1234
12345
Click me to see the sample solution

37. Write a C++ program that makes a pattern such as a right angle triangle using numbers that repeat.
Sample Output:

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

38. Write a C++ program to make such a pattern like a right angle triangle with the number increased by 1.
Sample Output:

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

39. Write a C++ program to make such a pattern like a pyramid with numbers increased by 1.
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 C++ program to make such a pattern like a pyramid with an asterisk.
Sample Output:

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

41. Write a C++ program to make such a pattern, like a pyramid, with a repeating number.
Sample Output:

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

42. Write a C++ program that displays the pattern like a pyramid using asterisks, with odd numbers in each row.
Sample Output:

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

43. Write a C++ program to print Floyd's Triangle.
Sample Output:

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

44. Write a C++ program to display a pattern like a diamond.
Sample Output:

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

45. Write a C++ program to display Pascal's triangle like a pyramid.
Sample Output:

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

46. Write a C++ program to display Pascal's triangle like a right angle triangle.
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   1
Click me to see the sample solution

47. Write a program in C++ to display such a pattern for n number of rows using numbers. Odd numbers will appear in each row. The first and last number of each row will be 1 and the middle column will be the row number.
Sample Output:

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

48. Write a program in C++ to display the pyramid pattern using the alphabet.
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 A
Click me to see the sample solution

49. Write a C++ program to print a pyramid of digits as shown below for n number of lines.

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

50. Write a C++ program to print a pattern in which the highest number of columns appears in the first row.
Sample Output:

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

51. Write a C++ program that displays the pattern with the highest columns in the first row and digits with the right justified digits.
Sample Output:

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

52. Write a C++ program to display the pattern using digits with left justified spacing and the highest columns appearing in the first row in descending order.
Sample Output:

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

53. Write a C++ program to display the pattern like right angle triangle with right justified digits.
Sample Output:

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

54. Write a program in C++ to display the pattern like pyramid, power of 2.
Sample Output:

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

55. Write a program in C++ to display such a pattern for n number of rows using numbers. There will be odd numbers in each row. The first and last number of each row will be 1 and the middle column will be the row number. N numbers of columns will appear in the 1st row.
Sample Output:

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

56. Write a program in C++ to find the first and last digits of a number.
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 the first and last digits of a number.
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 the product of the digits of any number.
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.
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.
Sample Output:
Input any number: 8309
Eight Three Zero Nine
Click me to see the sample solution

61. Write a C++ program that prints all ASCII characters with their values.
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 the power of any number using a for loop.
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.
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 the one's complement of a binary number.
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 the two's complement of a binary number.
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".
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+.......
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.
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.
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  0 
 
Click me to see the sample solution

70. Write a program in C++ to convert a decimal number to a binary number.
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 a hexadecimal number.
Sample Output:
Input a decimal number: 43
The hexadecimal number is : 2B
Click me to see the sample solution

72. Write a C++ program to convert a decimal number to an octal number.
Sample Output:
Input a decimal number: 15
The octal number is: 17
Click me to see the sample solution

73. Write a C++ program to convert a binary number to a decimal number.
Sample Output:
Input a binary number: 1011
The decimal number: 11
Click me to see the sample solution

74. Write a C++ program to convert a binary number to a hexadecimal number.
Sample Output:
Input a binary number: 1011
The hexadecimal value: B
Click me to see the sample solution

75. Write a C++ program to convert a binary number to an octal number.
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 C++ program to convert an octal number to a decimal number.
Sample Output:
Input any octal number: 17
The equivalent decimal number: 15
Click me to see the sample solution

77. Write a C++ program to convert an octal number to a binary number.
Sample Output:
Input any octal number: 17
The equivalent binary number: 1111
Click me to see the sample solution

78. Write a C++ program to convert an octal number to a hexadecimal number.
Sample Output:
Input any octal number: 77
The hexadecimal value of 77 is: 3F
Click me to see the sample solution

79. Write a C++ program to convert a hexadecimal number to a decimal number.
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 C++ program to convert a hexadecimal number to a binary number.
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 C++ program to convert a hexadecimal number to an octal number.
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.
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 C++ program to compute the sum of the digits of an integer.
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 C++ program to compute the sum of the digits of an integer using a function.
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.
Sample Output:
Enter a string: w3resource The string in reverse are: ecruoser3w
Click me to see the sample solution

86. Write a C++ program to count the letters, spaces, numbers and other characters in an input string.
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 C++ program to create and display an original three-digit numbers using 1, 2, 3, 4. Also count how many three-digit numbers are there.
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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/for-loop/index.php