1. You are given a number n representing the length of a floor space which is 2m wide. It's a 2 * n board.
2. You've an infinite supply of 2 * 1 tiles.
3. You are required to calculate and print the number of ways floor can be tiled using tiles.
Input Format
A number n
Output Format
A number representing the number of ways in which the number of ways floor can be tiled using tiles.
Constraints
1 <= n <= 100
Sample Input
8
Sample Output
34
Solution:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int prev = 1, next = 1, sum = 0;
for(int i=2;i<=n;i++){
sum = prev + next;
prev =next;
next = sum;
}
System.out.println(next);
}
}
Comments
Post a Comment