logo codesdope
Level 1
1.
Write a function to calculate area and perimeter of a rectangle.

2.
Write a function to calculate area and circumference of a circle.

3.
Write a function to calculate power of a number raised to other. E.g.- ab.

4.
Write a function to tell user if he/she is able to vote or not.
( Consider minimum age of voting to be 18. )

5.
Print multiplication table of 12 using recursion.
Answer
def table(n,i)
  puts n*i
  i=i+1
  if i<=10
    table(n,i)
  end
end
table(14,1)
  
									

6.
Write a function to calculate power of a number raised to other ( ab ) using recursion.
Answer
def power(a,b)
  if b == 1
    return a
  else
    return a*power(a,b-1)
  end
end
puts power(6,2)
  
									

7.
Write a function “perfect()” that determines if parameter number is a perfectnumber. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].
Answer
def perfect(n)
  sum = 0
  for i in (1..n-1)
    if n%i == 0
      sum = sum + i
    end
  end
  if sum == n
    return true
  else
    return false
  end
end
for i in (1..1000)
  if perfect(i)
    puts i
  end
end
  
									

8.
Write a function to check if a number is even or not.

9.
Write a function to check if a number is prime or not.

10.
Write a function to find factorial of a number but also store the factorials calculated in a dictionary as done in the Fibonacci series example.
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.