0

hello , i'm trying to solve this problem

Create a program which generates fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8 (beginner)

the problem is that the output is 0 1 1 2 3 5 8 13 , but it should be 0 1 1 2 3 5 8 . Can somebody help me?)

#include <cstdlib>
#include <iostream>
#include <conio.h>

     
using namespace std;
int main() 
{
	
	int a=10,sum = 0, b = -1 ,c = 1;
	
	while(sum<a) {
	
	sum = (b+c);
	cout<<sum<<" ";
	b=c;
	c=sum; 	
	}
	
	
 
getch();
return 0;
}

p.s the 'n' number is 'a' .

Edited by Dakot: n/a

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by WaltP
0

On your intended last iteration, line #12 will test true; however, line #14 will perform one additional calculation that you probably don't want it to do and will subsequently print the result (line #15.)

One solution could be to change your loop to a do/while so the evaluation to continue the loop would occur after the line #14 computation.

Edited by Clinton Portis: Fibotastically Fiboniaccilicious. (say it 5 times fast)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.