Question

Make a Code in C++ language. The Code should not copy paste from internet. The code...

Make a Code in C++ language. The Code should not copy paste from internet. The code should complete and should run. Also provide output. Make the code in easy way and also provide comments. The code should complete and Don't forget to provide output.

1. Make a cpp class TTT.
2. In that class, you will have two functions. one main function and one function named fib().
3. Get the value of n from user in main function. n specifies the number of elements to be displayed in fibonnaci numbers.
4. pass that value of n to fib(). and then by recursion keep printing the fibonacci sequence.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

answer to the above question is attached below : -

c++ code : -

#include <iostream>
using namespace std;
class TTT //class declarations
{
   //public Functions()
   public:
   int fib(int n) //recursive method to find nth fibpnacci number
   {
   if (n==0) //if n is 0 return 0
   return 0;
   else if (n==1) //if n is 1 return 1
   return 1;
   return (fib(n-1) + fib(n-2)); //return fib(n-1)+fib(n-2)

   }
   //main method for TTT class
   void main() {
   int n; //variable declaration
   cout<<"Enter the numeber of elements in sequence"<<endl;
   cin>>n; //taking user input
   int i=0; //variable
//    printing fibonacci number
   while(i<n) //while i is less than n
   {
   cout<<fib(i)<<" "; //call fib function for each value of i
   i++; //increment in i
   }
}
};
// main driver method of c++ program
int main() {
   // Declare an object of class TTT
   TTT obj;
   // accessing member function
obj.main();
   return 0;
}

code screenshot : -

code output : -

if my answer hellped then pleae upvote and comment for any queries

Thankyou !

Add a comment
Know the answer?
Add Answer to:
Make a Code in C++ language. The Code should not copy paste from internet. The code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT