Write a Java program which will generate threads to display 10 terms of Fibonacci Series and to display 1 to 20 in Reverse order.
Write a Java program which will generate threads to display 10 terms of Fibonacci Series and to display 1 to 20 in Reverse order.
Program:
import java.lang.*;
class FibonacciReverse extends Thread{
String str;
FibonacciReverse(String s){
str = s;
setName(str);
start();
}
public void run() {
int a=1,b=1,i,j;
int c=a+b;
try {
for(i=0;i<=10;i++) {
a=b;
b=c;
c=a+b;
System.out.println(c);
sleep(500);
}
} catch(InterruptedException e) {
System.out.println(e);
}
try {
for(j=20;j>=1;j--) {
System.out.println(j);
sleep(500);
}
} catch (InterruptedException e){
System.out.println(e);
}
}
}
class ThreadMain {
public static void main(String args[]){
FibonacciReverse t1 = new FibonacciReverse("first");
FibonacciReverse t2 = new FibonacciReverse("second");
}
}
Comments
Post a Comment