Practice questions on if and else

Level 1

Take values of length and breadth of a rectangle from user and check if it is square or not.
puts "Enter length"
length = gets.chomp.to_i
puts "Enter breadth"
breadth = gets.chomp.to_i
if length == breadth
  puts "Yes, it is square"
  puts "No, it is only Rectangle"

Take two int values from user and print greatest among them.
puts "Enter first number"
first = gets.chomp.to_i
puts "Enter second number"
second = gets.chomp.to_i
if first>second
  puts "Greatest is",first
elsif second>first
  puts "Greatest is",second
  puts "Both are equal"

A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
puts "Enter quantity"
quantity = gets.chomp.to_i
if quantity*100 > 1000
  puts "Cost is #{((quantity*100)-(0.1*quantity*100))}"
  puts "Cost is",quantity*100

A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
puts "Enter salary"
salary = gets.chomp.to_i
puts "Enter year of service"
yos = gets.chomp.to_i
if yos>5
  puts "Bonus is #{0.05*salary}"
  puts "No bonus"

A school has following rules for grading system:
a. Below 25 - F
b. 35 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
puts "Enter marks"
marks = gets.chomp.to_i
if marks<25
  puts "F"
elsif marks>=25 and marks<45
  puts "E"
elsif marks>=45 and marks<50
  puts "D"
elsif marks>=50 and marks<60
  puts "C"
elsif marks>=60 and marks<80
  puts "B"
  puts "A"

Take input of age of 3 people by user and determine oldest and youngest among them.
puts "Enter first age"
first = gets.chomp.to_i
puts "Enter second age"
second = gets.chomp.to_i
puts "third age"
third = gets.chomp.to_i

if first >= second && first >= third
  puts "Oldest is #{first}"
elsif second >= first && second >= third
  puts "Oldest is #{second}"
elsif third >= first && third >= second
  puts "Oldest is #{third}"
  puts "All are equal"

#find youngest yourself
#After this also check if two are equal, then which two				

Write a program to print absolute vlaue of a number entered by user. E.g.-
INPUT: 1        OUTPUT: 1
INPUT: -1        OUTPUT: 1
puts "Enter a number"
number = gets.chomp.to_i
if number<0
  puts number*-1
  puts number

A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
puts "Number of classes held"
noh = gets.chomp.to_i
puts "Number of classes attended"
noa = gets.chomp.to_i
atten = (noa/noh.to_f)*100
puts "Attendence is #{atten}"
if atten >= 75
  puts "You are allowed to sit in exam"
  puts "Sorry, you are not allowed. Attend more classes from next time."

Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
#Hint only #Do it yourself if medical_cause == 'Y' puts "You are allowed" else if atten>=75 puts "Allowed" else puts "Not allowed" end end

Level 2

Write a program to check if a year is leap year or not.
If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.
Use % ( modulus ) operator.

Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.
if employee is female, then she will work only in urban areas.

if employee is a male and age is in between 20 to 40 then he may work in anywhere

if employee is male and age is in between 40 t0 60 then he will work in urban areas only.

And any other input of age should print "ERROR".
puts "Enter age"
age = gets.chomp.to_i
puts "SEX? (M or F)"
sex = gets.chomp
puts "MARRIED? (Y or N)"
marry = gets.chomp
if sex == "F" && age>=20 && age<=60
  puts "Urban areas only"
elsif sex == "M" && age>=20 && age<=40
  puts "You can work anywhere"
elsif sex == "M" && age>40 && age<=60
  puts "Urban areas only"
  puts "ERROR"

A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.-
INPUT : 1234        OUTPUT : 4321
INPUT : 5982        OUTPUT : 2895
puts "Enter number"
number = gets.chomp.to_i

#seperating digits of the number
#1234/1000 = 1
#1234%1000 = 234

first_number = number/1000
rem = number%1000

#234/100 = 2
#234%100 = 34
second_number = rem/100
rem = rem%100

#34/10 = 3
#34%10 = 4
third_number = rem/10
fourth_number = rem%10

#creating new number with digits reversed
#1234 = (1*1000)+(2*100)+(3*10)+4 i.e., 1000+200+30+4
new_number = (fourth_number*1000)+(third_number*100)+(second_number*10)+(first_number)
puts new_number

Level 3

Ask Yours
Post Yours