• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Fibonacci

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to change this code to produce output like Example
If the user enters 6, program should display the numbers 1 1 2 3 5 8 not just 8.
If the user enters 2, program should display the numbers 1 1 not just 1.
I am have troubles can any one help. This assigment is killing me I am sure it is just some thing I am over looking.


//************************************************************
//Fibonacci.java
//Todd M. Smith
//Intro to Java Programming CIPT 190
//Lab 13
//A program that uses a recursive method to display the first
//numbers of the Fibonacci sequence input by the user.
//************************************************************

import java.io.*;

public class Fibonacci
{
public static void main(String args[]) throws IOException
{
int theNum, theFib;

//***************************************************
// Set up buffer reader
//***************************************************
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));

//***************************************************
// Get first Fibonacci number from user
//***************************************************
System.out.print("Enter Fibonacci number: ");
theNum = Integer.parseInt(stdin.readLine());

theFib = fib(theNum);

System.out.println("The " + theNum + "th Fibonacci " +
"number = " + theFib + ".");
}
static int fib(int k) {

// Base Case:
// If k <= 2 then fib(k) = 1.
if (k <= 2) {
return 1;
}
// Recursive Case:
// If k > 2 then fib(k) = fib(k-1) + fib(k-2).
else {
return fib(k-1) + fib(k-2);
}
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use loop to print all the values.

Try with the following code:

import java.io.*;

public class Fibonacci
{
public static void main(String args[]) throws IOException
{
int theNum, theFib;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Fibonacci number: ");
theNum = Integer.parseInt(stdin.readLine());

for(int p=1;p<=theNum;p++)
{
theFib = fib(p);
System.out.print(theFib+", ");
}

}

static int fib(int k)
{
if (k <= 2) {
return 1;
}
else {
return fib(k-1) + fib(k-2);
}
}
}

I have just added the loop statement to your code.

Hope this helps.

 
Your buns are mine! But you can have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic