Sunday, December 6, 2020

C# Program to generate Fibonacci Triangle

 In this program, we are getting input from the user for the limit for fibonacci triangle, and printing the fibonacci series for the given number of times (limit).

Let's see the C# example to generate fibonacci triangle.

  1. using System;  
  2.   public class PrintExample  
  3.    {  
  4.      public static void Main(string[] args)  
  5.       {  
  6.        int a=0,b=1,i,c,n,j;      
  7.        Console.Write("Enter the limit: ");    
  8.        n= int.Parse(Console.ReadLine());     
  9.        for(i=1; i<=n; i++)      
  10.        {      
  11.         a=0;      
  12.         b=1;      
  13.         Console.Write(b+"\t");     
  14.         for(j=1; j<i; j++)      
  15.         {      
  16.          c=a+b;      
  17.          Console.Write(c+"\t");      
  18.          a=b;  
  19.          b=c;  
  20.         }      
  21.         Console.Write("\n");      
  22.        }      
  23.    }  
  24.   }  

Output:

Enter the limit: 9
1	
1	1	
1	1	2	
1	1	2	3	
1	1	2	3	5	
1	1	2	3	5	8	
1	1	2	3	5	8	13	
1	1	2	3	5	8	13	21	
1	1	2	3	5	8	13	21	34


No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...