Question about Fibonacci Sequence.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 700 Views - Last Post: 03 February 2013 - 10:22 AM Rate Topic: -----

#1 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Question about Fibonacci Sequence.

Posted 15 January 2013 - 09:10 AM

Can someone help me to ge the sequence code to get this output using C++?
When I input 1 the output should be:

1
1
2
3
5
8
13
21
34
55
89
Is This A Good Question/Topic? 0
  • +

Replies To: Question about Fibonacci Sequence.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: Question about Fibonacci Sequence.

Posted 15 January 2013 - 09:11 AM

What have you tried? You need to post your code showing how you are trying to solve this issue.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Question about Fibonacci Sequence.

Posted 15 January 2013 - 09:45 AM

The sequence of numbers is called the Fibonacci Sequence. There's plenty of online resources explaining it.

This post has been edited by Aphex19: 15 January 2013 - 03:12 PM

Was This Post Helpful? 1
  • +
  • -

#4 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 15 January 2013 - 02:50 PM

I'll post it later. I'm at work right now and don't have my app here.
But what I did is just showing:

1
2
3
4
5
6
7
8
9
10
11

Can't understand how to insert the sequence
Was This Post Helpful? 0
  • +
  • -

#5 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 15 January 2013 - 02:57 PM

Thanks guys for the reply!
I got the website now.
I really appreciate your reply.
Was This Post Helpful? 0
  • +
  • -

#6 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 08:44 AM

I'm trying to create a fibinocci sequence. This is the code that I created:
#include<stdio.h>
#include<conio.h>

main()

{
int var,a=0,b=1,c,sum=1,d;
clrscr();
printf("Enter an integer: ");
scanf("%i",&var);
var=d;

   for(var=1;var<=11;var++)
	{
	c=a+b;
	sum=sum+c;
	printf("\n%i",c);
	b=c;
	a=b;
		if(c==d)
		break;
		else
		continue;
		}
getch();
}


The output is just showing: 1,2,4,8,16,31,64....
The Output that I need is: 1,1,2,3,5,8,13,21,34,55,89,

Pls reply....

This post has been edited by jimblumberg: 19 January 2013 - 08:52 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 08:55 AM

Please don't open a new topic for an existing problem. And please learn to use code tags. Topics merged.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 08:59 AM

Look at lines 18 and 19. You're setting a to the value of c.
Was This Post Helpful? 1
  • +
  • -

#9 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 11:36 AM

Sorry jim, just a newbie on this site. Hope you understand.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1304
  • View blog
  • Posts: 4,488
  • Joined: 19-February 09

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 11:48 AM

It can help if you try and create variables with descriptive names.

You have :

09	printf("Enter an integer: ");
10	scanf("%i",&var);




I think you want the maximum value of Fibonacci number to print. So something like :

09	printf("Enter the maximum value of Fibonacci number to print : ");
10	scanf("%i", &max_fibonacci);





Wikipedia said:

By definition, the first two numbers in the Fibonacci sequence are 0 and 1 (alternatively, 1 and 1), and each subsequent number is the sum of the previous two.


so something like :

  subsequent_number = previous_1 + previous_2;
  /* or */
  current_number = previous_1 + previous_2;
  /* or */
  fibonacci_number = previous_1 + previous_2;


This post has been edited by #define: 19 January 2013 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 11:53 AM

@CTphpnwb - what should I put? really can't get it.
Was This Post Helpful? 0
  • +
  • -

#12 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 02:00 PM

I was able to get the right code now. But the output is not showing after I type some number. I can just see the output once I run it again.
#include<stdio.h>
#include<conio.h>
main()
{
int num=0,a=0,b=1,count=0;

printf("How many number you want? (3minimum):");
scanf("%d",&num);
printf("0\n1");
do{b=a+b;a=b-a;b=a+b-a;count++;
printf("\n%d",B)/>/>;
}
while(count<=num-3);
printf("\n");
return 0;
}
getch();

This post has been edited by jimblumberg: 19 January 2013 - 07:14 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: Question about Fibonacci Sequence.

Posted 19 January 2013 - 07:15 PM

Please use code tags! :code:

Jim

This post has been edited by jimblumberg: 19 January 2013 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 johnpaulbb02  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: Question about Fibonacci Sequence.

Posted 20 January 2013 - 02:30 AM

Still the same. it's not showing right away after I type some number.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: Question about Fibonacci Sequence.

Posted 20 January 2013 - 07:14 AM

After I fix the compile errors your program seems to work for me.


You need to find an indentation style you like and use it consistently. This will make reading your program much easier.

Here is your code reformatted:
#include<stdio.h>
#include<conio.h>
main()
{
   int num=0,a=0,b=1,count=0;

   printf("How many number you want? (3minimum):");
   scanf("%d",&num);
   printf("0\n1");
   do {
      b=a+b;
      a=b-a;
      b=a+b-a;
      count++;
      printf("\n%d",b );
   }
   while(count<=num-3);
   printf("\n");
   return 0;
}
getch();



And here are the error messages I get when I compile your code:

Quote

||=== c_homework, Debug ===|
main.c|3|error: return type defaults to ‘int’|
main.c|21|error: data definition has no type or storage class|
main.c|21|error: type defaults to ‘int’ in declaration of ‘getch’|
main.c|21|warning: redundant redeclaration of ‘getch’ [-Wredundant-decls]|
conio.h|28|note: previous declaration of ‘getch’ was here|
||=== Build finished: 4 errors, 1 warnings ===|


Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2