Fibonacci series in java

Share Your Love

Hello guys , welcome to Today we are going to discuss the Fibonacci series in java.

import java.util.Scanner;

public class Fibonacci_series {

public static void main(String[] args) {
int n,a=0,b=1,c;
Scanner Sc=new Scanner(;
System.out.println("enter the value of n");
n=Sc.nextInt( );
for(int i=1;i<=n;i++) {
System.out.println(a+" ");



 ❖ Package;

            It is a java package( which is a collection of classes and this package is created in a java project .


❖ import java.util.Scanner;

             Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings .


❖ public class  Fibonacci_series

            *Here   Fibonacci_series  is defined as a class which is a template used to create objects and to define object data types and methods . >–and it’s defined as public because it can be accessed anywhere .


 ❖ public static void main(String[] args)

             >>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method. 

           //If a method is not public, then access is restricted and its showing error //static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance

         //void it means no return type

         //main() is function string[]args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly


 ❖ int n,a=0,b=1,c;

        >>– here we declare n  as a variable which is integer type,and take the  value of a=0,  similarly the value of b is 1  and c is a variable.


 ❖ Scanner sc=new Scanner(;

          >>–it means here scanner is a class and Sc is the instance of the object which is new is an argument 


❖ System.out.println(“Enter the value of n “);   

          >>– it means System is a class name and out is an instance of the System println is print a new line .


  •  n=sc.nextInt();

         >>– here n is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.


  •  for=(int i=1;i<=n;i++){

   >>while means we are using a while loop and then(n>=1) means the value of n is greater or equal to 1.


❖ System.out.println(a+” “);

                >>– it means System is a class name and out is an instance of the System println is print a new line .


  • c=a+b;



        >>c=a+b   means the sum of a and b  values is equal to the value of c.

        >> a=b     means the value of b is equal to the value of a.

        >>b=c      means the value of c is equal to the value of b.

Share Your Love
Avatar photo

Myself Sonalin Shaoo. I am quite happy to contribute here some java programming content and my friends Lipsa Mohanty ,Sushil Puhan, Aswin Kumar Nayak also help me to contribute their Opinion on this .

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter