Write a Java Program to print Fibonacci series up to 10
public class Fibonacci {
public static void main(String[] args) {
int num = 10;
System.out.println("*****Fibonacci Series*****");
int f1=0, f2=0, f3=1;
for(int i=1;i<=num;i++){
if(f2<=10) {
System.out.print(" "+f2+" ");
}
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
Output:
*****Fibonacci Series*****
0 1 1 2 3 5 8
public class Fibonacci {
public static void main(String[] args) {
int num = 10;
System.out.println("*****Fibonacci Series*****");
int f1=0, f2=0, f3=1;
for(int i=1;i<=num;i++){
if(f2<=10) {
System.out.print(" "+f2+" ");
}
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
Output:
*****Fibonacci Series*****
0 1 1 2 3 5 8
0 comments:
Post a Comment