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

2.
Take two int values from user and print greatest among them.
Answer
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
else
  puts "Both are equal"
end
									

3.
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.
Answer
puts "Enter quantity"
quantity = gets.chomp.to_i
if quantity*100 > 1000
  puts "Cost is #{((quantity*100)-(0.1*quantity*100))}"
else
  puts "Cost is",quantity*100
end
									

4.
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.
Answer
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}"
else
  puts "No bonus"
end
									

5.
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.
Answer
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"
else
  puts "A"
end
									

6.
Take input of age of 3 people by user and determine oldest and youngest among them.
Answer
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}"
else
  puts "All are equal"
end

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

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

8.
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.
Answer
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"
else
  puts "Sorry, you are not allowed. Attend more classes from next time."
end
									

9.
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.
Answer
#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
1.
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.
Hint
Use % ( modulus ) operator.

2.
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".
Answer
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"
else
  puts "ERROR"
end
									

3.
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
Answer
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
									
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.