Program that uses while loops to calculate the first n Fibonacci numbers
When I run it and input a number it just repeats it over non-stop. for example if i put a 3 it will do this 3 3 3 3 3 BUT NON STOP
int main()
{
int current=0, prev=1, prev2=1, fibnum;
cout << "Enter the number of F开发者_如何转开发ibonacci numbers to compute: ";
cin >> fibnum;
if (fibnum <=0)
{
cout << "Error: Enter a positive number: ";
}
while (fibnum > 0){
current = prev + prev2;
prev = prev2;
prev2 = current;
current++;
cout << "," << fibnum;
cout << endl;
}
return 0;
}
There are several problems with the code:
- You never assign anything to
fibnum
inside the body of the loop, so its value never changes. - The purpose of
current++
is entirely unclear.
Basically, you need to decide on the exact meaning of every variable, and stick to it throughout. The way these variables are being used, there's clearly confusion around the purpose of current
and fibnum
.
#include <iostream>
using namespace std;
int main(){
int current=0, prev=0, prev2=1, fibnum;
cout << "Enter the number of Fibonacci numbers to compute: ";
cin >> fibnum;
if (fibnum <=0){
cout << "Error: Enter a positive number: ";
}
while (fibnum--){
cout << prev ;
current = prev + prev2;
prev = prev2;
prev2 = current;
if(fibnum)
cout << ",";
}
cout << endl;
return 0;
}
change to
int current_fib_num = 0;
....
while (current_fib_num++ != fibNum)
{
....
// your code here
}
In addition to previous answers note that you can use benefits of recursion if you need to calculate fib number with some certain number. Something like that:
#include <cstddef>
std::size_t fib( std::size_t num )
{
// For first two numbers
if (num <= 2)
return 1;
return fib(num - 1) + fib(num - 2);
}
but you must keep in mind that this will lead to redundant calculations 'coz of repeatable recalc of same numbers and stack use for transmitting function args.
You are trying to print fibnum, but it is not changing inside the while loop. You should be printing current instead. Also you need to set a counter that will see the end of while loop.
#include <iostream>
using std::cin;
using std::cout;
int main()
{
int a=1, b=1, nums_to_print;
while (1) {
cout << "Enter the number of Fibonacci numbers to compute: ";
cin >> nums_to_print;
if (nums_to_print > 0) {
while (1) {
cout << a;
b += a;
a = b - a;
if (--nums_to_print) cout << ",";
else break;
}
cout << "\n";
return 0;
}
cout << "Error: Enter a positive number.\n";
}
}
Demo: http://ideone.com/3H8Fq
There are a couple of things you need to fix.
You need to have a count variable;
int current=0, prev=0, prev2=1, fibnum;
int count;
....
To output the first number before the loop
cout<<prev2;
You can change this to a for loop to make it easier to count the numbers
for(count = 0; count <= fibnum; count++){
current = prev + prev2;
prev = prev2;
prev2 = current;
You need to print current, not fibnum -> fibnum is the total numbers that you need to print
cout << "," << current;
}
#include <iostream>
using namespace std;
int main()
{
int i=0,j=1;
int c,n,count=0,d;
cout<<"enter num";
cin>>n;
c=i+j;
cout<<i<<j;
while(count<n-2)
{ d=j+c;
cout<<d;
j=c;
c=d;
count++;
}
return 0;
}
精彩评论