Rajat Malakar
Official Website
Tuesday, 28 August 2018
Tuesday, 9 February 2016
Monday, 22 July 2013
Using While loop Find the Fibonacci series in Java.
// Rajat Malakar
import java.util.*;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int prev, next, sum, n = 1, count;
System.out.println("Enter number upto which Fibonacci series to print: ");
count = sc.nextInt();
prev = next = 1;
System.out.println(">>>>>> : Fibonacci Series : <<<<<<");
while (n <= count) {
System.out.println(prev);
sum = prev + next;
prev = next;
next = sum;
n++;
}
}
}
import java.util.*;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int prev, next, sum, n = 1, count;
System.out.println("Enter number upto which Fibonacci series to print: ");
count = sc.nextInt();
prev = next = 1;
System.out.println(">>>>>> : Fibonacci Series : <<<<<<");
while (n <= count) {
System.out.println(prev);
sum = prev + next;
prev = next;
next = sum;
n++;
}
}
}
Subscribe to:
Posts (Atom)