MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

C# program to print fibonacci series

A prime number is not divisible by any other number apart from 1 and itself. So, we use this logic in this program to determine if a number is prime.



using System;


namespace SamplePrograms
{
    class FibonacciSeries
    {
        public static void Main()
        {
            // Prompt the user to enter their target number
            Console.WriteLine("How many numbers do you want in the fibonacci series");


            // Read the user input from console and convert to integer
            int Target = int.Parse(Console.ReadLine());


            // Create integer variables to hold previous and next numbers
            int PreviousNumber = -1, NextNumber = 1;


            // This for loop controls the number of fibonacci series elements
            for (int i = 0; i < Target; i++)
            {
                // Logic to compute fibonacci series numbers
                int Sum = PreviousNumber + NextNumber; 
                PreviousNumber = NextNumber;
                NextNumber = Sum;
                Console.Write(NextNumber + "  ");
            }


            Console.ReadLine();
        }
    }
}
A prime number is not divisible by any other number apart from 1 and itself. So, we use this logic in this program to determine if a number is prime.

2 comments:

  1. print stars in diamond,find number of vowels in a string

    ReplyDelete
    Replies
    1. Find No of vowel :-

      public int NoOfVowel(string str)
      {
      int i = 0;
      char[] cArray = str.ToCharArray();

      i = cArray.Count(a => a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u');

      return i;
      }

      Delete