Class Exercise on For loop
For Loop:
1. Write a program to print the numbers from 2 to 12 in reverse order.
2. Write a program to print all the odd numbers from 1 to n.
3. Write a program to print all the even numbers from 1 to n.
4. Write a program to print the square of all numbers from 1 to n.
5. Write a program to print first 5 multiples of a number n.
6. Write a program to print the square of all odd numbers from 1 to n.
7. Write a program to print all the numbers from 1 to n which are divisible by both 3 and 5.
8. Write a program to print all the numbers from 1 to n which are divisible by either 3 or 5.
9. Write a program to print all the numbers from 1 to n which are divisible by 7 or end by 7.
10. Write a program to print all the two digited even numbers.
11. Write a program to print all the two digited palindrome numbers.
12. Write a program to print the series 1,4,7,10,13,16.
13. Write a program asking the user to enter starting value, ending value and difference and
print the series.
For eg. : if s=4, e=15 and d=3 then o/p is 4,7,10,13
14. Write a program to print the square of all odd numbers from 1 to n.
15. Write a program to print the table of 2 from 1 to 10 , in following format :
2 X 1= 2
2 X2 = 4
2 X 3= 6
.
.
.
2 X 10 = 20
16. Write a program to print all the odd and even numbers from 1 to n .
If n = 10
Then output should be :
Od Even
d
1 2
3 4
5 6
7 8
9 10
17. Write a program to enter a string and print all the charaters of string in vertical line.
For eg. If str=”HELLO”
Then output is : H
E
L
L
O
18. Write a program to display table of n from 1 to m.
19. Write a program to enter a string and display only capital letters from the string.
20. Write a program to print the capital alphabets, small alphabets , digits along with there
ordinal values, you can use multiple loops.
21. Write a program to print all the leap year between 1900 and 2000.
22. Write a program to enter start value in s, number of terms in n and difference in d and
generate the series.
For eg. : s= 4, n=5, d=4
Then o/p is 4 8 12 16 20
23. Write a program to enter the start value, end value and step value , and print the series
accordingly.
For eg. Start = 5, end = 15, step = 2
5 7 9 11 13 15
24. Write a program to print all two digited numbers which end with 7 or are divisible by 7.
25. Write a program to print all 3 digited palindrome numbers. (Palindrome numbers are those
numbers whose reverse is same as original number)
(the output should be 101,111,121,131,141,151,161,171,181,191,202,212……)
26. The production (P) of crude oil of a country in millions of barrels may be estimated by the
following set of equations, where t represents the time in years.
P = 5+3t, for 0≤ t≤3
P = 14+(t-5/2)2, for t>3
Write a program in python to find the quantity of production for every year from t = 1 to 10
and print it
27. Write a program to find the sum of first n odd numbers .
28. Write a program to find sum of all odd numbers between 1 to n
29. Write a program to find factorial of a number .
30. Write a program to generate the following series : 1 4 9 16 25 ….. upto n terms.
31. Write a program to print every integer between 1 to n divisible by m. Also report whether
the number that is divisible by m is even or odd.
32. Write a program to generate the series : 1 3 5 7 …. (2n-1)…. Upto n terms
33. Write program to enter marks of n students and count number of students who passes and
number of students who failed, assume marks are out of hundred and passing criteria is 35.
34. Write a program to accept the age of n employees and count the number of employees in
following age groups : (i) 26-35 (ii) 36-45 (iii) 46-55
35. Write a program to generate the series : 1 2 4 7 11 16…. Upto n terms
36. Write a program to enter a number n, and checks and displays if it is a perfect numbers or
not. A perfect number is a number which is sum of all its factors.
37. Write a program using for to find the value xn, here x, n are entered by user.
If x= 2, n= 3 then result = 8
38. Write a program to enter sales made by n salesman print the total sales and average sales
made.
39. Write a program to enter marks of n students and print total and average marks.
40. Write a program to generate the following series :
(i) 1 4 7 10 13…. Upto n terms
(ii) 1 -4 7 -10 13…. Upto n terms
41. Write a program to find sum of the following series :
(i) Sum = 1+ 1 + 1 + 1 +……..upto n terms
4 7 10
(ii) Sum = 2 – 5 + 8 + …… upto n terms
9 13 17
(iii) Sum = 12+ 22+32+ ……….n2
(iv) Sum = 12+ 32+52+ ……….n2
42. Write a program to find the sum of the following series :
(i) Sum = x+x2 + x3 + x4 +….. + xn
(ii) Sum = 1! + 2! +3! + …… + n!
43. Write a program to print the Fibonacci series upto n terms ,
for eg, if n=6 then output is : 0 1 1 2 3 5
44. Write a program to enter a number and check if it is prime number or not.
45. Write a program in C++ to find the perfect numbers between 1 and 500.
46. Write a program to print all prime numbers between 1 to n.
47. Write a program in C++ to display the n terms of 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
48. Write a program in C++ to find the number and sum of all integer 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
49. Write a program to find sum of following series : (Use nested for loop)
(i) Sum = x+ x2 + x3 + x4 + x 5 - …….+ xn
2! 3! 4! 5! n!
(ii) Sum = x- x2 + x3 - x4 + x 5 - …….+ xn
3! 5! 7! 9! (2n-1)!
50. Write a program in C++ to calculate the series
(i) Sum = (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n).
(ii) Sum = (12) + (12+ 32) + (12+ 32+ 52)……. Upto n terms
51. Draw following pyramids using nested for loop (consider n=4, for programs 1 to 12)
1. 1 2. 1 3. 1 4. a
12 22 21 ab
123 333 321 abc
1234 4444 4321 abcd
5. 1 6. # 7. 1 8. 1
121 ### 123 13
12321 ##### 12345 135
1234321 ####### 1234567 1357
9. 1 10. 1 11. a 12. @@@@
01 12 bc @@@
101 123 def @@
0101 1234 ghij @
123
12
1
13. a 14. 1 15. 1 16. 1234
ce 35 44 123
gIk 7 9 11 999 12
moqs 13 15 17 19 16 16 16 16 1
12
123
1234
52. Write a program to enter a string and generate following pyramid’s :
For eg If str=’Hello’
1. H 2. Hello 3. olleH 4. o
He Hell olle ol
Hel Hel oll oll
Hell He ol olle
Hello H o olleH
54. Write a program to keep on entering marks of n students and print the highest and lowest
marks.
53. Write a program to keep on entering the sales made by n salesman and print the top two
sales made.
54. Write a program enter a 5-digited number and print its sum , use for loop.
55. Write a program to enter any number (can be in string form) and print its sum.