Program to Display Fibonacci Series

Description

The Fibonacci series is a sequence where the next term is the 
sum of the previous two terms. The first two terms of the Fibonacci
series are 0 followed by 1.

Example:
0 1 1 2 3 5 8 13 21 34....

C/C++

/* C Program to Display Fibonacci Series */
//Save it as FibonacciSeries.c

#include<stdio.h>
int main(){

    int i, n, a=0, b=1, c, num;

    printf("Enter a number terms : ");
    scanf("%d",&num);

    printf("Fibonacci series is : \n");

    for(i=1;i<=num;i++){
        printf("%d ",a);
        c = a+b;
        a = b;
        b = c;
    }

    return 0;
}
Input:
Enter a number terms : 10

Output:
Fibonacci series is : 
0 1 1 2 3 5 8 13 21 34

Java

/* Java Program to Display Fibonacci Series */
//Save it as FibonacciSeries.java

import java.io.*;
import java.util.Scanner;

public class FibonacciSeries {

    public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);
        
            int i, n, a=0, b=1, c, num;

        System.out.println("Enter a number terms : ");
        num = scanner.nextInt();

        System.out.println("Fibonacci series is : ");

        for(i=1;i<=num;i++){
            System.out.print(a+" ");
            c = a+b;
            a = b;
            b = c;
        }
    }
}
Input:
Enter a number terms : 
10

Output:
Fibonacci series is : 
0 1 1 2 3 5 8 13 21 34

Related Programs

1) Program to Display Fibonacci Series using Recursion
2) Program to calculate Factorial of a Number
3) Program to display multiplication table of a number
4) Program of Simple Calculator using switch
5) Program to find HCF using Recursion
6) program to calculate square root of a number using sqrt()
7) Program to find largest and smallest character in a String
8) Program to add two complex numbers
9) Program to swap two strings
10) Program To Check Armstrong Number
Share Me

Leave a Reply