fibonacci sequence

I wrote this code for the first 25 terms of the fibonacci sequence beginning with 1. It works fine, but I need it as a while loop and nothing I tried works. How do I change it over to a while loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main () 
{
    int j=1, k=1, sum;
	
	for (int i=0; i<25; i++) 
	{
		cout << j << " ";
		sum=j+k;
		j=k;
		k=sum;
	}
    return 0;
}


If you just want a simple while loop you could do something like this.

1
2
3
4
5
6
7
8
9
10
11
12

	int i = 0;  

	while(i < 25)
        {
		cout << j << " ";
		sum=j+k;
		j=k;
		k=sum;

		i++; 
	}
Last edited on
Topic archived. No new replies allowed.