Friday, June 9, 2017

Ways to repay loan

A poor man borrows a sum of N rupees from his neighbour. The neighbour is kind enough not to impose any interest on the loan. The poor man agrees to re-pay the loan by paying back either 1 rupee or 2 rupees to his neighbour. In how many distinct ways can he repay the loan? 

Input Format: 

The first line will contain the value of N which is the loan amount. 

Output Format: 

The first line will contain the number of distinct ways in which the loan can be repaid.  

Example Input/Output 1: 

Input: 

Output: 

Example Input/Output 3: 

Input: 

Output: 

Explanation: 
The loan can be re-paid as 
1,1,1,1,1 or 
1,1,1,2 or 
1,2,2 or 
1,1,2,1 or 
1,2,1,1 or 
2,2,1 or 
2,1,2 or 
2,1,1,1

Solution: 


n = int(input())
a = 0
b = 1
i = 0
while i<n:
    c = a+b
    a = b
    b = c
    i += 1
print(c)

Share: