Created
May 3, 2019 15:31
-
-
Save jatinsharrma/77834c79f47ede45feaaa2baf8ec6bce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A C program to count number of ways to reach n't stair when | |
// a person can climb 1 or 2 stairs at a time. | |
//if code is wrong or i misssed a case please let me know. If code can be optmised or logic can be reformed please let me know. | |
#include<stdio.h> | |
int one = 0; | |
int two = 0; | |
int count = 0; | |
int fact(int n){ | |
int fact = 1; | |
for (int i =1; i<=n;i++){ | |
fact = fact *i; | |
} | |
return fact; | |
} | |
int stairs(int n, int two, int one){ | |
if(one == 0 || one ==1){ | |
count = 1; | |
return count; | |
} | |
else{ | |
stairs(n-1,two+1,(one-2)); | |
count = count + fact(n-1)/(fact(two+1)*fact(one-2)); | |
return count; | |
} | |
} | |
int main(){ | |
int n = 20; | |
printf("%d",stairs(n,two,n)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment