Close
Close

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
  puts "Enter number"
  num = gets.chomp.to_i
  sum = sum + num
  i = i-1
end
puts "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
  puts "*"*i
  i = i+1
end

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

#c
#Do yourself

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

4.
Write an infinite loop.
A inifinte loop never ends. Condition is always true.
while true
  puts "INFINITE"
end

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! = 0
Write a program to calculate factorial of a number.
puts "Enter number"
number = gets.chomp.to_i
fac = 1
if number == 0
  print 1
else
  while number>=1
    fac = fac*number
    number = number-1
  end
  puts fac
end

6.
Write a program to find greatest common divisor (GCD) or highest common factor (HCF) of given two numbers.
puts "Enter first number"
x = gets.chomp.to_i
puts "Enter second number"
y = gets.chomp.to_i
#x,y = 10, 20 means x = 10 y = 20
while y != 0
  x, y = y, x % y
end
puts 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
puts "Enter a number"
number = gets.chomp.to_i
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
  end
end
puts 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 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
puts "Enter number"
number = gets.chomp.to_i
i = 2
while i <= number
  #Checking if prime or not
  j = 2
  count = 0
  while j <= i
    if i%j == 0
      count = count+1
    end
    j = j+1
  end
  #if count > 1 then it is not prime
  #if prime and factor(number%i==0) then it is prime factor
  if count <= 1 && number%i == 0
    puts i
  end
  i = i+1
end

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

Level 3

Ask Yours
Post Yours