Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,731 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,315 people online right now. Registration is fast and FREE... Join Now!




fibonacci with optional starting number and number series

 
Reply to this topicStart new topic

fibonacci with optional starting number and number series

daniel
5 Feb, 2007 - 11:06 PM
Post #1

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 3


My Contributions
I'm a beginner with C and I have an assignment due tomorrow and I've spent 3 hours trying to figure out how to do this problem.... I am really stumped where to go from here, I keep getting "illegal else without matching if", please I need your help! i made only series of number but not the starting!!

QUOTE
#include<stdio.h>
#include<conio.h>

long fibo (int no);
main()
{
int n;
clrscr();
printf("\n Enter a number of sequence:");
scanf("%d", &n);
fibo(n);
getch();
}

long fibo (int no)
{
long int i,p,p1,p2;
p1=1;
p2=1;
p=1;
for(i=1;i<=no;i++){
printf(" %d",p);
if(i>=1){
p=p1+p2;
p2=p1;
p1=p;
}
else if(i<=0)
{
printf("not");
}


}
}



Attached thumbnail(s)
Attached Image Attached Image
User is offlineProfile CardPM
+Quote Post

Videege
RE: Fibonacci With Optional Starting Number And Number Series
5 Feb, 2007 - 11:19 PM
Post #2

rêvant.toujours
Group Icon

Joined: 25 Mar, 2003
Posts: 1,406


Dream Kudos: 150
My Contributions
There's no need to triple post. Please only post your question in one thread; I have removed the other two duplicates.

Thanks.
User is offlineProfile CardPM
+Quote Post

horace
RE: Fibonacci With Optional Starting Number And Number Series
6 Feb, 2007 - 12:41 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
I compiled and ran you program without problems, e.g. a run gave
Enter a number of sequence: 6
1 2 3 5 8 13
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Fibonacci With Optional Starting Number And Number Series
6 Feb, 2007 - 12:47 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
Well the code you entered is ok but you need one change that fibonacci series is like 1 1 2 3 5 8 .... and not 1 2 3 5 ...
so make changes in your code accordingly.

and about the input as starting number for fibonacci series then all you need to do is pass the number to your function and assign that number to the variables p, p1 & p2.

CODE

void fibo (int no,int start)
{
  long int i,p,p1,p2;
  p1=start;
  p2=start;
  p=start;
  printf("%d",p);
  for(i=1;i<no;i++)
  {
    printf(" %d",p);
    p=p1+p2;
    p2=p1;
    p1=p;    
  }
}


well about the validations given in thumbnails provided by you, then those can be done in the caller function like main.

Hope your problem is solved. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 05:14AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month