Fibonacci series up to a certain number in JAVA programming


  1. import java.util.Scanner;
  2.  
  3.  
  4. public class FibonacciUpToACertainNum {
  5.  
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner myScanner=new Scanner(System.in);
  10.  
  11. System.out.print("Enter a number : ");
  12.  
  13. int n=myScanner.nextInt();
  14.  
  15. int first=0,second=1,sum;
  16.  
  17. System.out.print("Fibonacci series up to "+n+" is : ");
  18.  
  19. System.out.print(first+"\t"+second+"\t");
  20.  
  21. sum=first+second;
  22.  
  23.  
  24.  
  25. while(sum<=n){
  26.  
  27. System.out.print(sum+"\t");
  28.  
  29. first=second;
  30.  
  31. second=sum;
  32.  
  33. sum=first+second;
  34.  
  35. }
  36.  
  37. }
  38.  
  39.  
  40. }
  41.  
  42. //Enter a number : 10
  43.  
  44. //Fibonacci series up to 10 is : 0 1 1 2 3 5 8

No comments:

Post a Comment