Tuesday, July 25, 2023

Fibonacci series

 

import java.util.*;

 

public class Main

{

                public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in); 

                                 System.out.println( " enter a value");

 

                     int n1=0,n2=1,n3,i,count;

                     count = sc.nextInt();   

 

   

 for(i=1;i<=count;++i)//loop starts from 2 because 0 and 1 are already printed   

 {   

  n3=n1+n2;   

  System.out.print(" "+n1);   

  n1=n2;   

  n2=n3;   

 }   

 

}} 

 

No comments:

Post a Comment