Practice questions on While...

Level 1

1.
Take 10 integers from keyboard using loop and print their average value on the screen.
```sum = 0

i = 10
while i>0:
print "Enter number"
num = input()
sum = sum + num
i = i-1
print "average is",sum/10.0
```

2.
Print the following patterns using loop :
a.
*
**
***
****
b.
*
***
*****
***
*
c.
1010101
10101
101
1
```#a
i = 1
while i<=4:
print "*"*i
i = i+1

#b
i = 1
j = 2
while i>=1:
a =  " "*j+"*"*i+" "*j
print a
i = i+2
j = j-1
if i>5:
break
i = 3
j = 1
while i>=1:
a =  " "*j+"*"*i+" "*j
print a
i = i-2
j = j+1

#c
#Do yourself
```

3.
Print multiplication table of 24, 50 and 29 using loop.
```i = 1
while i<=10:
print 24*i
i = i+1
```

4.
Write an infinite loop.
A inifinte loop never ends. Condition is always true.
```while True:
print "INFINITE"
```

5.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 1
Write a program to calculate factorial of a number.
```print "Enter number"
number = input()
fac = 1
if number == 0:
print 1
else:
while number>=1:
fac = fac*number
number = number-1
print fac
```

6.
Write a program to find greatest common divisor (GCD) or highest common factor (HCF) of given two numbers.
```print "Enter first number"
x = input()
print "Enter second number"
y = input()
#x,y = 10, 20 means x = 10 y = 20
while y != 0:
x, y = y, x % y
print x
```

7.
Take integer inputs from user until he/she presses q ( Ask to press q to quit after every integer input ). Print average and product of all numbers.
```# Do it yourself
# This type of example is discussed in tutorial
```

Level 2

1.
Calculate the sum of digits of a number given by user. E.g.-
INUPT : 123        OUPUT : 6
INUPT : 12345        OUPUT : 15
```print "Enter a number"
number = input()
summ = 0
#number%10 will give last digit of number
#number = number/10 will give new number without that digit
#we will stop when number will be smaller than 10
while True:
r = number%10
number = number/10
summ = summ+r
if number < 10:
summ = summ+number
break
print summ
```

2.
A three digit number is called Armstrong number if sum of cube of its digit is equal to number itself.
E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153.
Write all Armstrong numbers between 100 to 500.
```#Find seperate digits as done in previous question
#then find the sum of cubes
```

3.
Write a program to print a number given by user but digits reversed. E.g.-
INPUT : 123        OUTPUT : 321
INPUT : 12345        OUTPUT : 54321

4.
Write a program to find prime factor of a number.
If a factor of a number is prime number then it is its prime factor.
This is not an efficent algorithm. You can also use sieve algorithm to find prime numbers. You can see this answer
```print "Enter number"
number = input()
i = 2
while i <= number:
#Checking if prime or not
j = 2
count = 0
while j <= i:
if i%j == 0:
count = count+1
j = j+1
#if count > 1 then it is not prime
#if prime and factor(number%i==0) then it is prime factor
if count <= 1 and number%i == 0:
print i
i = i+1
```

5.
Write a program to print all prime number in between 1 to 100.