Fibonacci Series in java

CODE :

a)
WHILE LOOP

public class P_04_fibb_while_loop {
public static void main(String[] args) {
int a=1,temp=0,b=0;
System.out.println(a);
while (a<10) {
temp=a+b;
b=a;
a=temp;
System.out.println(temp);
temp++;
}
}

}

b)
DO WHILE LOOP

public class P_04_do_fibb_while_loop {
public static void main(String[] args) {
int a=1,temp=0,b=0;
System.out.println(a);
do{
temp=a+b;
b=a;
a=temp;
System.out.println(temp);
temp++;
}while (a<10);
}

}

c)
FOR LOOP

public class P_04_fibb_for_loop {
public static void main(String[] args) {
int a=1,temp=0,b=0;
System.out.println(a);
for(;a<10;temp++) {
temp=a+b;
b=a;
a=temp;
System.out.println(temp);
}
}

}


Output :



Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex