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' .

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.

thanks, i'll try :)

Run your loop by hand at your desk with pencil and paper.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.