Skip to main content

Write a recursive program that finds out ifn is a Fibonacci number or not. The Fibonacci numbers are 0 1 1 2 3 5 13 21 Your program should have the following interface: Enter a number (enter a negative number to quit): 14 !!!!! Sorry14 is not a Fibonacci number Enter a number (enter a negative number to quit): 13 Yes, you got it, 13 is a Fibonacci number Enter a number (enter a negative number to quit): 22 !!!!! Sorry 22 is not a Fibonacci number Enter a number (enter a negative number to quit): 23 !!!!! Sorry 23 is not a Fibonacci number Enter a number (enter a negative number to quit): 21 Yes, you got it, 21 is a Fibonacci number Enter a number (enter a negative number to quit): -1 (*Thanks Have a good Day*)

Question

I have the following code:

 

#include <iostream>

#include <cstdlib>

 

using namespace std;

 

//main method

 

int main()

 

{

 

    //variable declaration

 

        int num, num1 = 0, num2 = 1, temp;

 

        // getting input from user

 

       cout<<"Enter a number (enter a negative number to quit): ";

 

        cin>>num;

 

        //If the number entered by the user is negative then exit the program

 

        if (num<0)

 

        {

 

            //exit statement

 

            exit(0);

 

        }

 

        // 0 and 1 are fibonacci numbers

 

        if (num1==num || num2==num)

 

        {

 

                cout << num <<" is a fibonacci number\n";

 

                return 0;

 

        }

 

 

 

        // checking whether a given number is Fibonacci or not

 

        while (num2 <= num)

 

        {

 

            //assigning the value of the second number to a temp variable

 

                temp = num2;

 

                //assigning the sum value of num1 and num2 to num2

 

                num2 = num1 + num2;

 

                //assigning temp varible value to num1

 

                num1 = temp;

 

                //if num2 is equal to the number entered by the user

 

                if (num2 == num)

 

                {

 

                        break;

 

                }

 

        }

 

        // printing the results

 

        if (num1==num||num2==num)

 

        {

 

            //displaying a message to the user that entered number is a Fibonacci number

 

           cout<<"Yes, you got it, " <<num<< " is a Fibonacci number";     

 

        }

 

        else

 

        {

 

            //displaying a message to the user that the entered number is not a Fibonacci number

 

                cout<<"!!!!! Sorry "<<num <<" is not a Fibonacci number";

 

        }

 

        return 0;

  }

 

but I need it to output like exact attached image, where I can keep inputing numbers until it quits and can pleases how the recursive formula is determing FIB nums 

Write a recursive program that finds out ifn is a Fibonacci number or not. The Fibonacci
numbers are
0 1 1
2
3
5
13
21
Your program should have the following interface:
Enter a number (enter a negative number to quit): 14
!!!!! Sorry14 is not a Fibonacci number
Enter a number (enter a negative number to quit): 13
Yes, you got it, 13 is a Fibonacci number
Enter a number (enter a negative number to quit): 22
!!!!! Sorry 22 is not a Fibonacci number
Enter a number (enter a negative number to quit): 23
!!!!! Sorry 23 is not a Fibonacci number
Enter a number (enter a negative number to quit): 21
Yes, you got it, 21 is a Fibonacci number
Enter a number (enter a negative number to quit): -1
(*Thanks Have a good Day*)
expand button
Transcribed Image Text:Write a recursive program that finds out ifn is a Fibonacci number or not. The Fibonacci numbers are 0 1 1 2 3 5 13 21 Your program should have the following interface: Enter a number (enter a negative number to quit): 14 !!!!! Sorry14 is not a Fibonacci number Enter a number (enter a negative number to quit): 13 Yes, you got it, 13 is a Fibonacci number Enter a number (enter a negative number to quit): 22 !!!!! Sorry 22 is not a Fibonacci number Enter a number (enter a negative number to quit): 23 !!!!! Sorry 23 is not a Fibonacci number Enter a number (enter a negative number to quit): 21 Yes, you got it, 21 is a Fibonacci number Enter a number (enter a negative number to quit): -1 (*Thanks Have a good Day*)
Expert Solution

Want to see the full answer?

Check out a sample Q&A here
Blurred answer
Students who’ve seen this question also like:
Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Not helpful? See similar books
Database System Concepts
Database System Concepts
Introduction. 1PE
marketing sidebar icon
Want to see this answer and more?
Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: C++    Write a recursive program that finds out if n is a Fibonacci number or not. The Fibonacci…

A: The C++ program will follow the given rubrics:Importing header files.Displaying the message to user…

Q: Using recursion, write a Java program that takes an input ‘n’ (a number) ( user should gives the n…

A: Input : n : Input number from the user. Output : Output the n Fibonacci number based on the…

Q: Write a recursive Java method that calculates the sum of n positive integer numbers. The math…

A: Note:- please mention type of error which you have faced in your post.  Here is logic:-   public…

Q: Write a recursive method that gives the following pattern(Write in Java language) run : 1 2 4 7 11…

A: Here the series is generated as: 1+0 = 11+1 = 22+2 = 44+3 = 77+4 = 11....   Here in the main method,…

Q: Write a recursive method that takes two integers n and m as parameters, where m is one digit number.…

A: Program Approach: Import the necessary package Declaring the main class Declaring the method…

Q: Write a recursive method that takes two integers n and m as parameters, where m is one digit number.…

A: Since the programming language is not mentioned, so we'll do it in java. The programming methodology…

Q: Write an application that reads a positive whole number (n) and print Fibonacci (Fibo) series: 0, 1,…

A: here have to determine about  c# code for get fibinacci numbers.

Q: Write an application that reads a positive whole number (n) and print Fibonacci (Fibo) series: 0, 1,…

A: To Do: To write the c# code.

Q: Write a recursive program in Java to find the sum of integers from -100 to 0 and display the sum in…

A: Write a recursive program in Java to find the sum of integers from -100 to 0 and display the sum in…

Q: Write a recursive method using java to return the sum of all numbers less or equal n divisible by 2…

A: Recursion is a basic programming technique you can use in java in which a method calls itself to…

Q: Write a recursive method using java to return the sum of all numbers less or equal n divisible by 2…

A: Java program to build a recursive function that returns the sum of all elements that is divisible by…

Q: Using recursion, write a Java program that takes an input ‘n’ (a number) from a user to calculate…

A: The Java Fibonacci recursion function takes an input number. Checks for 0, 1, 2 and returns 0, 1, 1…

Q: Write a recursive method in java that returns a string that contains the octal representation of a…

A: program in java user has to input number in decimal program will calculate it in octal using…

Q: Write a recursive method in java that returns a string that contains the octal representation of a…

A: import java.io.*; public class Octal{    // Function to convert decimal to octal    static void…

Q: Write a recursive method that will compute the number of even digits in a number.

A: "Since you have not mention any programming language, so will write our program in C language".   C…

Q: Write a recursive method called digitCount() that takes a positive integer as a parameter and…

A: code : -       import java.util.Scanner; public class Main { /* TODO: Write recursive digitCount()…

Q: Java language Write a recursive method to add all of the odd numbers between two numbers (start and…

A: Actually, java is a object oriented programming language. It is a platform independent.

Q: Write a program that uses a recursive function, decToBin, to convert a non- negative integer in…

A: As no programming language is mentioned, it is solved using basic C++

Q: Write a recursive method called digitCount() that takes a positive integer as a parameter and…

A: According to the Question below the Solution:   Output:

Q: Note: Java Consider the following recursive method:             public static int Fun(int x) {…

A: NOTE: We’ll answer the first question since the exact one wasn’t specified. Please submit a new…

Q: QI. Write a java program using recursion to find the power of a number x", where x is double and n…

A: Recursion is a process of calling the same function itself

Q: QI. Write a java program using recursion to find the power of a number x", where x is double and n…

A: As per our company guidelines, we are supposed to answer only one question per post and kindly…

Q: How to write a java program that return the nth Fibonacci number when passed the argument using a…

A: 1.Program having both recursive and non-recursive methods: File name: “FibonacciCalc.java” class…

Q: write a recursive methodcalled doubleDigits that accepts an integer n as a parameter and returns the…

A: import java.util.*; import java.util.Scanner; public class DoubleDigits { public static void…

Q: • Write a recursive method to complete the class BaseRaiseToN. • The method: public static int…

A: Program code: //import the required packages import java.util.Scanner; //define the class…

Q: Write a recursive method called reverseString() that takes in a string as a parameter and returns…

A: import java.util.Scanner;public class LabProgram   {  //recursive function to reverse a string…

Q: Write a recursive method that for a positive integer returns a string with commas in the appropriate…

A: Given:- Write a recursive method that for a positive integer returns a string with commas in the…

Q: Give the correct output for the recursive code below: public class Fibonacci { public static void…

A: About the output for the recursive code given

Q: create a Java recursive method that determines if a number is a prime number

A: import java.util.Scanner;class prime_number{ public static void main(String args[]) {…

Q: Write a recursive method largestDigitthat accepts an integer parameter and returns the largest digit…

A: if (num == 0) return 0; num = Math.abs(num); int lastNum = num % 10;…

Q: Recursively Write a JavaScript program to compute the exponent of a number.

A: We have to write a recurssive function to find the exponent in Javascript

Q: Recursively Write a JavaScript program to compute the exponent of a number.

A: Please find the answer below :

Q: Recursively Write a JavaScript program to compute the exponent of a number.

A: &lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt; &lt;p&gt;Javascript exponent&lt;/p&gt; &lt;p…

Q: Recursively Write a JavaScript program to compute the exponent of a number.

A: &lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt; &lt;p&gt;Javascript exponent&lt;/p&gt; &lt;p…

Q: In java Reprogram the following iterative function with recursion.      int f(int N) {     int…

A: The equivalent recursive function for the given iterative function is as follows, //Define recursive…

Q: Write a program using recursion that computes the sum of the series: 1^2 + 5^2 + 6^2 + 11^2 + 17^2 +…

A: since you have not mentioned any programming language, I am attempting this in python. If you need…

Q: Write a program using recursion that computes the sum of the series: 1^2+5^2+ 6^2 + 11^2 + 17^2+28^2…

A: The Answer is in Below Steps

Q: Write a program using recursion that computes the sum of the series: 1^2 + 5^2 + 6^2 + 11^2 + 17^2 +…

A: #include &lt;iostream&gt;#include &lt;cmath&gt;using namespace std; int seriesSum(int i, int j, int…

Q: Write a program using recursion that computes the sum of the series: 1^2 +5^2 + 6^2 +11^2 +…

A: The Answer is in Below  steps

Q: Write a program using recursion that computes the sum of the series: 1^2+5^2 +6^2+11^2+ 17^2 +28^2…

A: The Answer is in Below Steps  solving in Python Program

Q: Write a recursive java language program in which user enters a string and an integer and and the…

A: Required:- Write a recursive java language program in which the user enters a string and an integer…

Q: Write a program that lists all ways people can line up for a photo (all permutations of a list of…

A: Code: from itertools import permutations name = input()  # To read the name names = [] names =…

Q: Rewrite the private static int recurseMax method without using Java API public class Recursion{…

A: import java.util.*;import java.lang.*;import java.io.*; class Codechef{ public static int large(int…

Q: Write a program that implements a recursive algorithm that prints the factorial of the first 10…

A: public class FacttTen {     public static Long fact(Long number) {        if(number == 1){…

Q: On RecursiveAppend.java write a recursive method appendNTimes that receives two arguments, a string…

A: public static String appendNTimes(String original, int n) {  if(n == 0)   return original;  else…

Q: Trace the following recursion code. Draw and Write down the outputs. Please, show the recursive flow…

A: Here hMB() is a recursive function, in which we call this function again until we reach on base…

Q: In C++ Write a program that lists all ways people can line up for a photo (all permutations of a…

A: The answer is

Q: A recursive method with no  base case?    1.causes an infinite loop 2.generates no output 3.does not…

A: A recursive method with no base case: 1.causes an infinite loop   As, if there is no base case, then…

Q: NAME: Fibonacci Using methods, write a syntactically and semantically a correct Java program in the…

A: Java program to solve the given problem is below.

Q: Write a RECURSIVE method called “sequence” that takes a single int parameter (n) and returns the int…

A: Please find the answer below :

Knowledge Booster
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
  • Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
  • C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education
  • Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education