C# Program for Count ways to reach the n'th stair - GeeksforGeeks

02 Jan, 2019
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top.
Consider the example shown in diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is taken from Easier Fibonacci puzzles
C#
// C# program to count the// number of ways to reach// n'th stairusing System;  class GFG {    // A simple recursive    // program to find n'th    // fibonacci number    static int fib(int n)    {        if (n <= 1)            return n;        return fib(n - 1) + fib(n - 2);    }      // Returns number of ways    // to reach s'th stair    static int countWays(int s)    {        return fib(s + 1);    }      // Driver Code    static public void Main()    {        int s = 4;        Console.WriteLine("Number of ways = " + countWays(s));    }}  // This code is contributed// by akt_mit
Number of ways = 5

The time complexity of the above implementation is exponential (golden ratio raised to power n). It can be optimized to work in O(Logn) time using the previously discussed Fibonacci function optimizations.
C#
// C# program to Count ways to reach// the n’th stairusing System;  class GFG {      // A recursive function used by    // countWays    static int countWaysUtil(int n, int m)    {        if (n <= 1)            return n;        int res = 0;          for (int i = 1; i <= m && i <= n; i++)            res += countWaysUtil(n - i, m);        return res;    }      // Returns number of ways to reach    // s'th stair    static int countWays(int s, int m)    {        return countWaysUtil(s + 1, m);    }      /* Driver program to test above function */    public static void Main()    {        int s = 4, m = 2;        Console.Write("Number of ways = "                      + countWays(s, m));    }}  // This code is contributed by nitin mittal.
Number of ways = 5

C#
// C# program to count number// of ways to reach n't stair when// a person can climb 1, 2, ..m// stairs at a timeusing System;class GFG {      // A recursive function    // used by countWays    static int countWaysUtil(int n, int m)    {        int[] res = new int[n];        res[0] = 1;        res[1] = 1;        for (int i = 2; i < n; i++) {            res[i] = 0;            for (int j = 1; j <= m && j <= i; j++)                res[i] += res[i - j];        }        return res[n - 1];    }      // Returns number of ways    // to reach s'th stair    static int countWays(int s, int m)    {        return countWaysUtil(s + 1, m);    }      // Driver Code    public static void Main()    {        int s = 4, m = 2;        Console.WriteLine("Number of ways = " + countWays(s, m));    }}  // This code is contributed by anuj_67.
Number of ways = 5

Please refer complete article on Count ways to reach the n’th stair for more details!
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Program to Check a Specified Type is an Enum or Not
C# Program to Read and Write a Byte Array to File using FileStream Class
C# Program to Sort a List of Integers Using the LINQ OrderBy() Method
How to Sort Object Array By Specific Property in C#?
C# Program to Join Multiple Data Sources using LINQ
How to Get a Comma Separated String From an Array in C#?
C# Program to Sort a List of String Names Using the LINQ OrderBy() Method
C# Program to Delete an Empty and a Non-Empty Directory
C# Program to Calculate the Sum of Array Elements using the LINQ Aggregate() Method
C# Program to Check a Specified Type is a Primitive Data Type or Not