Practice Sheet Loops-1
Practice Sheet Loops-1
1. Read a sequence of integers from the user until –1 is read. Output the sum of only
positive integers entered by the user.
2. Calculate the sum of squares and sum of cubes of the first N natural numbers.
3. Take n numbers as input from user. Find the maximum and minimum of those n
numbers.
4. Suppose you take a sequence of positive integers from the user till it enters -1. Find
the length of the longest contiguous subsequence in it. For example, if the user gave
the input 9,1,5,2,7,8,6 – the answer would be 3, for the subsequence 2,7,8.
5. Take a number as input from the user.
a. Calculate the sum of its digits. For example, for 6394, the answer is 6+3+9+4
= 22.
b. Output the number in reverse. For example, for 6394, the answer is 4936.
c. Find the value of the maximum digit. For example, for 6394, the answer is 9.
d. Add the odd position digits and subtract the even position digits. For
example, for 6394, the answer is +6-3+9-4=8.
6. Write a program to print the multiplication table of a user entered number. For
example, for n=7, output should be
8X1=8
8 X 2 = 16
...
8 X 10 = 80.
7. Ask the user to enter a GP (Geometric progression series). Test whether it is indeed
a GP. If yes, then print the sum of the GP (without using the formula). Verify, if the
sum indeed matches the value obtained using the GP sum formula.
8. Write a program to calculate sum of the Harmonic series, for first n terms, where n
is taken as input from the user.
9. Write a program to:
a. Test whether an input number is perfect or not. A number is perfect if equals
the sum of its positive divisors (except itself). For example 6 = 1+2+3
b. Print the first n perfect numbers. (using only a single loop)
c. Test whether an input number is Armstrong or not. A number is Armstrong if
it equals the sum of the cubes of its digits.
d. Print the first n Armstrong numbers. (using only a single loop)
10. Write a program to display the first n terms of the Fibonacci series. A fibonacci
series is one where every term is the sum of its previous two terms. The first two
terms are 0,1.