C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,530 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Take a look at this:
using System;
namespace ConsoleApp1
{
class Program
{
static void main(string[] args)
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
Console.WriteLine("Enter the number of terms: ");
n = Convert.ToInt32(Console.ReadLine());
// print the first two terms t1 and t2
Console.Write($"Fibonacci Series: {t1}, {t2}, ");
// print 3rd to nth terms
for (i = 3; i <= n; ++i)
{
Console.Write($"{nextTerm}, ");
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
}
}
}