Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FibonacciNumbers
- {
- class FibonacciNumbers
- {
- static void Main(string[] args)
- {
- //Write a program that reads a number N and prints on the console the first N members of the Fibonacci sequence (at a single line, separated by comma and space - ", ") : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ….
- //On the only line you should print the first N numbers of the sequence, separated by ", " (comma and space)
- //1 <= N <= 50 N will always be a valid positive integer number
- int n = int.Parse(Console.ReadLine());
- int firstNum = 0;
- int secondNum = 1;
- int thirdNum = 0;
- if (n>=1&&n<=50)
- {
- if (n == 1)
- {
- Console.WriteLine(firstNum);
- }
- else if (n == 2)
- {
- Console.WriteLine("{0}, {1}", firstNum, secondNum);
- }
- else if (n > 2)
- {
- Console.Write("{0}, {1}, ", firstNum, secondNum);
- for (int i = 1; i <= n - 2; i++)
- {
- thirdNum = firstNum + secondNum;
- firstNum = secondNum;
- secondNum = thirdNum;
- if (n - 2 == i)
- {
- Console.WriteLine(thirdNum);
- }
- else
- {
- Console.Write(thirdNum + ", ");
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment