Fibonacci series is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.
To find Fibonacci series upto a given number
public static void main(String[] args) {
public class Fibonacci {
int i = 1, n = 10, t1 = 1, t2 = 1;
System.out.println("First " + n + " terms: ");
while (i <= n)
{
System.out.println(" \t \t" + t1);
int sum = t1 + t2;
t1 = t2;
t2 = sum;
i++;
}
}
}
To Find using user input range
import java.util.*;
public class FibonacciUserInput {
public static void main(String[] args) {
int i = 1, n, t1 = 1, t2 = 1;
Scanner kb = new Scanner(System.in);
System.out.print("Enter the limit: ");
n = kb.nextInt();
System.out.println("First " + n + " terms: ");
while (i <= n)
{
System.out.println(" \t \t" + t1);
int sum = t1 + t2;
t1 = t2;
t2 = sum;
i++;
}
}
}
if you guys want us to post more about programming or codes please let us know either in the comment box or just mail us, so that we can start right away if you are interested.
No comments:
Post a Comment