Solved

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

Posted on 2008-10-14
4
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

623 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