Fibonacci Number
Fibonacci number is a number that start with one or zero and next number is equal to the sum of previous two numbers. Ex F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..
The first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.,
Example:-
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Source Code
package com.dsacode.Probelms; public class Fibonacci { public static int recursiveFibonacci(int num){ if(num==1 || num==2) return 1; else return recursiveFibonacci(num-1) + recursiveFibonacci(num-2); } public static int iterFibonacci(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; } public static void main(String[] args) { int numberOfFib =10; System.out.print("Print the first " + numberOfFib + " Fibonacci numbers using iterative funcation:"); for(int i = 1; i <= numberOfFib; i++){ System.out.print(iterFibonacci(i) +" "); } System.out.print(" \nPrint the first " + numberOfFib + " Fibonacci numbers using recursive funcation:"); for(int i = 1;i <= numberOfFib; i++){ System.out.print(recursiveFibonacci(i) +" "); } } }
#include "stdafx.h" #include < iostream > using namespace std; int recursiveFibonacci(int num){ if (num == 1 || num == 2) return 1; else return recursiveFibonacci(num - 1) + recursiveFibonacci(num - 2); } int iterFibonacci(int number){ if (number == 1 || number == 2){ return 1; } int fibo1 = 1, fibo2 = 1, fibonacci = 1; for (int i = 3; i <= number; i++){ fibonacci = fibo1 + fibo2; fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; } int _tmain(int argc, _TCHAR* argv[]) { int numberOfFib = 10; cout << "Print the first " << numberOfFib << " Fibonacci numbers using iterative funcation:"; for (int i = 1; i <= numberOfFib; i++){ cout << iterFibonacci(i) << " "; } cout << " \nPrint the first " << numberOfFib << " Fibonacci numbers using recursive funcation:"; for (int i = 1; i <= numberOfFib; i++){ cout << recursiveFibonacci(i) << " "; } return 0; }
Output
Print the first 10 Fibonacci numbers using iterative funcation:1 1 2 3 5 8 13 21 34 55 Print the first 10 Fibonacci numbers using recursive funcation:1 1 2 3 5 8 13 21 34 55
Algorithm Explanation
![]() | Get the number of Fibonacci numbers to display. . |
![]() | Use recursive function to decide the fib number. If the number is 1 or 2, return 1. |
![]() | Else call recursively with recursiveFibonacci(num-1) + recursiveFibonacci(num-2) |
32 Comments
As I website possessor I believe the content matter here is rattling magnificent , appreciate it for your hard work. You should keep it up forever! Good Luck.
Excellent post. I was checking continuously this blog and I’m impressed! Extremely helpful information particularly the last part 🙂 I care for such information much. I was seeking this certain information for a long time. Thank you and good luck.
I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 certain. Any suggestions or advice would be greatly appreciated. Thanks
The subsequent time I read a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to learn, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could repair if you werent too busy in search of attention.
Valuable info. Lucky me I found your web site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
whoah this weblog is magnificent i love studying your posts. Keep up the great paintings! You understand, many individuals are looking around for this info, you can aid them greatly.
magnificent points altogether, you simply gained a logo new reader. What may you recommend about your put up that you simply made a few days in the past? Any positive?
Spot on with this write-up, I truly assume this web site wants way more consideration. I’ll probably be once more to learn way more, thanks for that info.
whoah this weblog is excellent i like studying your articles. Stay up the good paintings! You understand, many individuals are hunting round for this info, you can aid them greatly.
My brother suggested I might like this blog. He was totally right. This post truly made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!
Just about all of what you state is astonishingly legitimate and that makes me ponder why I hadn’t looked at this in this light previously. Your article truly did switch the light on for me personally as far as this issue goes. Nonetheless there is actually just one factor I am not too comfortable with so while I attempt to reconcile that with the core theme of your point, allow me observe what all the rest of your readers have to say.Very well done.
Hey! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!
With the whole thing that appears to be developing throughout this particular subject matter, all your points of view are actually fairly stimulating. However, I beg your pardon, but I do not give credence to your entire strategy, all be it exciting none the less. It seems to us that your opinions are actually not entirely validated and in actuality you are yourself not even entirely certain of your point. In any event I did appreciate reading through it.
There are some interesting time limits on this article however I don’t know if I see all of them heart to heart. There is some validity however I’ll take maintain opinion until I look into it further. Good article , thanks and we would like more! Added to FeedBurner as effectively
Hi! Someone in my Myspace group shared this website with us so I came to look it over. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Great blog and fantastic style and design.
Aw, this was a really nice post. In concept I want to put in writing like this additionally – taking time and precise effort to make a very good article… but what can I say… I procrastinate alot and not at all seem to get something done.
I believe that is among the such a lot significant info for me. And i’m glad reading your article. However want to observation on few basic things, The site style is great, the articles is truly excellent : D. Good job, cheers
I do agree with all the ideas you have presented in your post. They’re very convincing and will certainly work. Still, the posts are too short for beginners. Could you please extend them a little from next time? Thanks for the post.
you might have a terrific weblog here! would you wish to make some invite posts on my weblog?
Wow! This blog looks just like my old one! It’s on a totally different topic but it has pretty much the same page layout and design. Wonderful choice of colors!
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate?
Amazing blog! Do you have any helpful hints for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips? Cheers!
fantastic put up, very informative. I ponder why the other experts of this sector do not understand this. You must proceed your writing. I’m confident, you’ve a huge readers’ base already!
you’re really a excellent webmaster. The web site loading velocity is incredible. It kind of feels that you’re doing any distinctive trick. Furthermore, The contents are masterwork. you’ve performed a excellent task in this topic!
Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch since I found it for him smile Therefore let me rephrase that: Thanks for lunch!
I enjoy what you guys are usually up too. This sort of clever work and exposure! Keep up the superb works guys I’ve incorporated you guys to blogroll.
Thank you for any other great post. Where else may anybody get that type of info in such a perfect method of writing? I have a presentation next week, and I am at the look for such info.
Hey there I am so excited I found your weblog, I really found you by accident, while I was looking on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to read it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job.
With havin so much content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help reduce content from being ripped off? I’d truly appreciate it.
I do believe all of the ideas you’ve presented in your post. They’re really convincing and can certainly work. Still, the posts are very short for beginners. May you please extend them a bit from next time? Thanks for the post.
Hey there! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to prevent hackers?
Really enjoyed this post, how can I make is so that I receive an email whenever you make a new post?