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");

}

}

}

Comments

Popular posts from this blog

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

Java program to find length of given string and convert it in to upper or lower case.

Java program to check marks out of Bound exception.

Java program to calculate area and perimeter of circle using interfaces.

Java program to merge two text files in to third file.

Java program to read a text file and displays it's contents on screen.

Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना

Java program to check greatest among three numbers where input numbers are given by user.

Write a Java program to calculate the gross salary of an employee, if basic salary of that employee is greater or equal to 20000 and year's of service is equal or greater than 5 years bonus will be 2000 else1000, TA =10%, DA= 20% and HRA=5% of salary.