You are on page 1of 1

public class StairwayToHeaven {

// Assuming 'n' is not below 1

private int stairwayToHeaven(int n){

// Initializing ways array

int[] ways = new int[n + 1];

ways[0] = 1;

ways[1] = 1;

for(int i = 2; i <= n; i++){

ways[i] = ways[i - 1] + ways[i - 2];

return ways[n];

public static void main(String[] args) {

StairwayToHeaven sth = new StairwayToHeaven();

System.out.println("Number of ways to reach heaven with 5 stairs is "


+ sth.stairwayToHeaven(5));

You might also like