Solved

How Do I Get This Fibonacci Program to Accept User Input?

Posted on 2008-10-14
4
228 Views
Last Modified: 2008-10-14
I am trying to develop a program that takes in a user input of a number and returns as an output that number in the fibonacci series.  For example, if the user types in 5, the 5th number is displayed; 23 and the 23rd number is displayed.  So far I have this, but it only spits out the fibonacci series for 100 numbers and does not stop at the number put in by the user.  Please help!

#include <stdio.h>

char line[1000];

int main()
{

  int fib[1000];
  int i;

  printf("Fibonacci\n");
  fgets(line, sizeof(line), stdin);
  sscanf(line," %d", &i);

  if (i<0) return 0;

  fib[0] = 0;
  fib[1] = 1;

  for (i = 2; i<1000; i++)
{
    fib[i] = fib[i-1] + fib[i-2];
    }

  for (i = 0; i<1000; i++)
    {
      printf("%d %d\n", i, fib[i]);
    }

  return (0);

}

0
Comment
Question by:spngegirl01
4 Comments
 

Author Comment

by:spngegirl01
ID: 22710778
Hi thank you that fixed the problem of having the output end after the term, however, the series now starts at 0 in the output, so when I enter Fibonacci 3, I really only see the series up to the second term (assuming the first term is 1).  I added this below, but it won't work.  Any ideas?


fib[0] ='\0';
  fib[1] = 1;
  fib[2] = 1;

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 22710803
spngegirl01,

Please disregard the code posted by shasunder and work on improving your own code which is good and can be easily improved.

>So far I have this, but it only spits out the fibonacci series for 100 numbers and does not stop at the number put in by the user

That happens because of this loop in which you are printing
  for (i = 0; i<1000; i++)
    {
      printf("%d %d\n", i, fib[i]);
    }
You are printing all values from 0 to 999 ... while you need to stop at the specific number. Edit your loop to iterate upto j times - that is the number of values you want printed - instead of 1000.

Next improvement that you can make is to calculate only j values instead of 1000 values. To do that - again make similar modifications to the loop in which you are calculating the values.

cheers!
sunnycoder
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 22714695
Sorry sunnycoder,

I was not aware that it was homework problem
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now