Solved

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

Posted on 2008-10-14
4
234 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

776 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