English, asked by MalayaDhal, 6 months ago

•○●Hey Guys●○•
\huge\star{\red{\fbox{This\:is\:the\:question}}}\star
Write a program in Java to enter a number and find out if the entered number is in fibonacci series or not.​

Answers

Answered by anindyaadhikari13
12

Answer:

Here comes the Java program for the question.

import java.util.*;

public class  CheckFibonacciNumber   {

  public static void main(String args[])  {

       int a=1,b=0,c=0,n;

       boolean x=false;

       Scanner sc=new Scanner(System.in);

       System.out.print("Enter a number - ");

       n=sc.nextInt();

       while(c<=n)   {

           if(n==c)  {

               x=true;

               break;

           }

           c=a+b;

           a=b;

           b=c;

       }

       if(x)

           System.out.println("Number is a Fibonacci Number.");

       else

           System.out.println("Number is not a Fibonacci Number.");

       sc.close();

   }

}

Algorithm:

  1. START.
  2. Initialise x=false.
  3. Accept the number, say n.
  4. Calculate all the fibonacci numbers till n.
  5. Check if that Fibonacci number is equal to n or not. If true, x=true. Break the loop.
  6. Check if x is true. If true, number is a fibonacci number else not.
  7. Display the result.

See the attachment for output.

Attachments:
Similar questions