Wednesday, 14 October 2020

WAP to print the following Fibonacci series 0 1 1 2 3 5 8 13 21 34 up to n times

 Fibonacci series up to n times


import java.util.Scanner;
class Fibo_range
{
 public static void main(String[] a)
 {
  int x = -1, y = 1, z;
  Scanner s=new Scanner(System.in);
  System.out.print("Enter range: ");
  int n = s.nextInt();
  for(int i = 1; i<=n; i++) {
   z = x+y;
   System.out.print(z+"  ");
   x = y;
   y = z;
  } } }



Output:
Enter range: 10
0  1  1  2  3  5  8  13  21  34  

No comments:

Post a Comment