Java program to find fibonacci series
import java.io.*;
import java.util.*;
class fibonacci
{
public static void main(String args[])
{
int num,num1=0,num2=1,num3;
System.out.println("Enter the limit\n");
Scanner sc= new Scanner(System.in);
num=sc.nextInt();
System.out.println(num1);
System.out.println(num2);
for(int i=2;i<num;i++) //already printed two numbers 0 and 1 that'swhy our i initiiialize from 2 skipping first two iteration
{
num3=num1+num2;
num1=num2;
num2=num3;
System.out.println(num3);
}
}
}
import java.util.*;
class fibonacci
{
public static void main(String args[])
{
int num,num1=0,num2=1,num3;
System.out.println("Enter the limit\n");
Scanner sc= new Scanner(System.in);
num=sc.nextInt();
System.out.println(num1);
System.out.println(num2);
for(int i=2;i<num;i++) //already printed two numbers 0 and 1 that'swhy our i initiiialize from 2 skipping first two iteration
{
num3=num1+num2;
num1=num2;
num2=num3;
System.out.println(num3);
}
}
}
OUTPUT-
Enter the limit
5
0
1
1
2
3
Comments
Post a Comment