Fibonacci Series in java
CODE :
a)
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)
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)
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 :
a)
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++;
}
}
}
DO 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 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
Post a Comment