Thursday, February 29, 2024

Java program to print fibonacci series of n numbers.

import java.util.*;

public class Fib{

public static void main(String args[]){

int a,b,c,n;

Scanner s=new Scanner(System.in);

System.out.println("How many terms do you want in fibonacci series\n");

n=s.nextInt();

if(n>0){

System.out.println("Enter initial two numbers\n");

a=s.nextInt();

b=s.nextInt();

System.out.println("Fibonacci series of"+n+ "term is:\n");

if(n==1){

System.out.println(a);

System.exit(0);}

if(n==2){

System.out.println(a+" "+b);

System.exit(0);}

System.out.print(a+" "+b+" ");

int i;

for(i=1;i<=n-2;i++){

c=a+b;

System.out.print(c+" ");

a=b;

b=c;

}

}

else{

System.out.println("Wrong number of term");

}

}

}

No comments:

Post a Comment

Java program to implement file input stream class to read binary data from image file.

import java.io.FileInputStream; import java.io.IOException; public class ReadImageFile {     public static void main(String[] args) {       ...