Write a program in C# Sharp to display the first n terms of Fibonacci series

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to display the first n terms of Fibonacci series

The series is as follows :
Fibonacci series 0 1 2 3 5 8 13 .....

Sample Output:

Display the first n terms of fibonacci series:                                                                                                         
------------------------------------------------                                                                                                         
Input number of terms to be display : 10                                                                                                         
Here is the fibonacci series upto  to 10 terms :                                                                                                         
0    1  1    2    3    5    8    13    21    34

الأجوبة

using System;  
public class Exercise35 
{  
    public static void Main()
{
   int prv=0,pre=1,trm,i,n;
   
	Console.Write("\n\n");
    Console.Write("Display the first n terms of fibonacci series:\n");
    Console.Write("------------------------------------------------");
    Console.Write("\n\n");     
   
   Console.Write("Input number of terms to be display : ");
   n = Convert.ToInt32(Console.ReadLine());    
   Console.Write("Here is the fibonacci series upto  to {0} terms : \n",n);
   Console.Write("{0}    {1}", prv,pre);
 
  for(i=3;i<=n;i++)
   {
     trm=prv+pre;
     Console.Write("  {0}  ",trm);
     prv=pre;
     pre=trm;
   }
   Console.Write("\n");
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...