Hi Sir, please help me with my code I've already finish the sequence.

this is my part :

#include <iostream.h>
int main ()
{
	 unsigned long int a = 0;
	 unsigned long int b = 1;
	 unsigned long int c;
	 int y = 2;
	 int x = 2;

	 cout << "How many Fibonacci number you need ? : " ;
	 cin >> y;
	 cout << a << "\t" << b;

	 do
	 {
		  x++;
		  c = a + b;
		  cout << "\t" << c;
		  a = b;
		  b = c;
	 }
	  while (x <= y);

	return 0;
}

now here the question: how choose the sequence output, what i want to is to ask the user "how many sequence he/she wants to see in my output,

Example:

how many Fibonacci number you need? : 10
0 1 1 2 3 5 8 13 21 34 55
(instead having this long numbers, i want to cut it down)

how many Fibonacci number you need? : 10
how many sequence: 3
21 34 55

this is it , thanks sir hope i can receive tips n how to do this thanks again

You want to act your program like this, right?

how many Fibonacci number you need? : 10
how many sequence: 3
21 34 55

Keep track of the amount of numbers already generated, when this amount exceeds [I]the_amount_of_fibonacci_numbers_you_need[/I] - [I]sequence[/I] , you start printing the numbers.
(In this case you would start printing the numbers at the moment when the generated fibonacci number exceeds 7 (10-3)).

An alternative could be to use an array and later print out the specific pieces that you need. tux's method works a lot cleaner but if you need to pick different points the sequance for later, having it stored can be helpful.

int y = 2;
int x = 2;
int* store_fib = int [y]
store_fib[0] = 0;
store_fib[1] = 1;
...
c = a + b;
store_fib[x-1] = c;
...
Comments
Good suggestion :)
This article has been dead for over six months. Start a new discussion instead.