10: Recursion

All the tasks in this chapter have to be solved using recursion.

10.1 Multiplication

Coding Exercise: Multiplication
Input two positive integer numbers. Write a program to multiply them. You are only allowed to use + and operations, i.e. multiplication and division operations are not allowed.
You may enter input for the program in the box below.

10.2 Reversed String

Coding Exercise: Reversed String
Given a string S. Write a program to reverse it.
You may enter input for the program in the box below.

10.3 Elfish Word

Coding Exercise: Elfish Word
A word is considered elfish if it contains the letters e, l and f in any order. For example, the following words are elfish:
whiteleaf, tasteful, unfriendly, waffles
Input a word. Output YES if the word is elfish and NO otherwise.
You may enter input for the program in the box below.

10.4 Prime

Coding Exercise: Prime
Input positive integer n. Write a program to check whether n ir prime or not. If n is prime, output YES, otherwise output NO.
You may enter input for the program in the box below.

10.5 Reversed number

Coding Exercise: Reversed number
Input a non-negative integer n. Write a program to reverse the number. For example, if n = 2560, the output should be 652.
You may enter input for the program in the box below.

10.6 Smallest element ir array

Coding Exercise: Smallest element ir array
Input n, the size of an array.
Input n elements – integer numbers – and store them in an array. Output the smallest value in the array.
You may enter input for the program in the box below.

10.7 Hanoi Towers

Coding Exercise: Hanoi Towers
The legend of the temple of Brahma tells:
  • There are 64 golden disks on one page sorted from the largest in the bottom to the smallest on the top.
  • There are three pegs in total (two others are initially empty).
  • The universe will end when the priest will move all the disks from the first peg to the last following the rules:
    • move one disk at a time
    • a move consists from taking one disk of a peg and putting it on another peg (on an empty peg or on top of other disks).
    • cannot put a larger disk on a top of a smaller disk.

hanoi

With 64 disks at 1 sec. per disk this would take roughly 585 billion years.
It is required to move all disks from peg A to peg B using peg C as the intermediate one. Write a program to input the number of disks n and to output all required moves.
For example, if n = 2, your output should be:
A -> C
A -> B
C -> B

You may enter input for the program in the box below.

10.8 Expression

Coding Exercise: Expression
Given a string which contains arithmetical expression consisting of positive numbers from 1 to 9 and arithmetical operations (+, -, *, /). The expression always starts from a positive number. Output the value of the arithmetical expression.
For example, if the given expression is:
2+5*6*3-2*5*6/3-2, The answer should be: 70
You may enter input for the program in the box below.