Loops Questions
Loops Questions
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
*
**
***
****
*****
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
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
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
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
40. Write a C++ program to make such a pattern like a pyramid with an
asterisk.
Sample Output:
Input number of rows: 5
*
* *
* * *
* * * *
* * * * *
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
42. Write a C++ program that displays the pattern like a pyramid using
asterisks, with odd numbers in each row.
Sample Output:
*
***
*****
*******
*
***
*****
*******
*********
*******
*****
***
*
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
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:
1
121
12321
1234321
123454321
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
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:
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
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
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
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
54. Write a program in C++ to display the pattern like pyramid, power of 2.
Sample Output:
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
01234
10123
21012
32101
43210
Sample Output:
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
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
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
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