Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Fibonacci program
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


j
Fibonacci program
1 Answer(s)      4 years and a month ago
Posted in : Java Beginners


I am trying to write 2 different Fibonacci programs. The first one, I want to allow the user to input 2 integers and then print out the next 10 numbers in the fibonacci sequence. On the next one, I want to allow the user to input 2 integers again and also allow the user to input how many fibonacci numbers they would like the program to print. I need to use a main method that calls the fibonacci method. Here is the code I have so far:

import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {

    Scanner s = new Scanner( System.in);
    System.out.println("How many fibonacci numbers would you like to print: ");
    int length = s.nextInt();

    long[] series = new long[length];

    series[0] = 0;
    series[1] = 1;



    //Scanner first = new Scanner( System.in);
    //System.out.println("Please enter the first number of the Fibonacci series: ");
    //firstInt = first.nextInt();
    //Scanner second = new Scanner( System.in);
    //System.out.println("Please enter the second number of the Fibonacci series: ");
    //secondInt = second.nextInt();

    //long series[0] = new long[firstInt];

    //long series[1] = new long[secondInt];



    //create the Fibonacci series and store it in an array
//public static int fib(int n)  
    for(int i=2; i < length; i++){
        series[i] = series[i-1] + series[i-2];
    }

    //print the Fibonacci series numbers
    System.out.println("Fibonacci series to " + length);
    for(int i=0; i< length; i++){
        System.out.print(series[i] + " ");
    }
}

}


Advertisement
View Answers

November 30, 2010 at 10:57 AM


Hi Friend,

Try the following code:

1)

import java.util.*;

public class Fibonacci {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("Please enter the first number of the Fibonacci series: ");
    int f1 = s.nextInt();
    System.out.println("Please enter the second number of the Fibonacci series: ");
    int f2 = s.nextInt();
    int f3=f1+f2;
    for(int i=1;i<=10;i++){
        System.out.println(f3);
        f1=f2;
        f2=f3;
        f3=f1+f2;
    }
}
}

2)

import java.util.*;

public class Fibonacci {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("How many fibonacci numbers would you like to print: ");
    int num = s.nextInt();
    int f1=0,f2=0,f3=1;
    for(int i=1;i<=num;i++){
        System.out.println(f3);
        f1=f2;
        f2=f3;
        f3=f1+f2;
    }
}
}

Thanks



Related Tutorials/Questions & Answers:
Fibonacci program
fibonacci numbers they would like the program to print. I need to use a main...Fibonacci program  I am trying to write 2 different Fibonacci... out the next 10 numbers in the fibonacci sequence. On the next one, I want
Fibonacci (Recursive)
Fibonacci (Recursive)  I need to write a program for my AP CS class... that give you the sequence of the Fibonacci numbers, but here is what it asks me... >= 0) and returns the appropriate Fibonacci number of the Fibonacci number
Advertisements
fibonacci series
fibonacci series  Program to print the first n fibonacci numbers using function.the program must use InputStreamReader and BufferedReader.  ...*; public class Fibonacci { public static void main(String[] args) throws Exception
Fibonacci series in java
value proceeding it. Here is the code for Fibonacci program: import...Fibonacci series in java In this section you will learn about fibonacci number in java.  Fibonacci number or Fibonacci sequence are the number
factorial of fibonacci
factorial of fibonacci   A code for the factorial of a fibonacci series. Example when the user enters 6, the fibonacci series is 8 and the factorial will be 8! or 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1. So if the user enters the number 6
sum of fibonacci series
sum of fibonacci series  Write a Java program to print Fibonacci series upto n and find their sum also. 0+1+1+2+3+5+8+13+21â?¦â?¦â?¦â?¦=sum   Hi, Please see the thread Fibonacci program. Thanks
fibonacci by using if/else - Framework
fibonacci by using if/else  Fib(n)=0 1 Fib(n-1)+Fib(n-2) if n=0 if n=1 Based on the above Fib() formula,write a C program that uses this main... a Fibonacci series.\n"); for(int looper=0;looper
fibonacci series logics
fibonacci series logics  A form contains 20 text fields, these fields.... Now we have fibonacci series number by which we have to find that numbers inserted by the user in fibonacci series or nearby. for each text field value. and show
fibonacci - Java Beginners
fibonacci  plss debug this program!!!! This program will diplay the fibonacci numbers when you input a number. import javax.swing.JOptionPane; public class Fibonacci { public static void main(String[] args) { int x
Prime And Fibonacci Number Finder
Prime And Fibonacci Number Finder In this section, you will learn how to find the prime numbers which are generated in the fibonacci series. To compute...() of boolean type. After that we have stored the fibonacci number series of 10
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
java program code
java program code  can any one write a program (class) which will have two methods Fibonacci and Factorial, which will be a void and a return respectively. i will be glad if it can have a good display results
program
program  any program of hotel or school management
program
program  write a program different between to dates in terms of days in java
program
program  write a html program in show table and this word in box
program
program  write a javascript program to create a application form with validation
program
program  program of jdbc using ms access for creating table
program
program  WAP a java program to form 1/2+3/4+5/6+7/8 series
program
program  develop a servlet to insert the data in the database from our program in the table stored in the database
program
program  write a program reversee a string. input-hi this is roseindia output-roseindia is this hi
program
program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
program
program   An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the program calculateTotalProfit that consumes
program
program  write a java program to input a no. and print wheather the no. is a special no or not. (special no. are those no whose factorial of the sum of digit of the no is same as the original
program
program  Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
Program
Program  a program to create two classes Commercial and Domestic. Override the method calculatebill() of Commercial class(Rs.8 per unit) into Domestic class(Rs 6 per unit) to compute electricity bill for both classes
Program
Program  a program to prove that default constructor of super class is available to subclass by default. Define a parameterize constructor for super class to store name = ??Tommy?? age=??23?? and call this constructor
program
program  how to improve programming skills plss tell some tips..i am understanding concepts but unable to write program so,pls give some guideness plsss
program
program  Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean
program
program  no i want a program that shows how merge sort algorithm works in graphical representations like numbers are taken in circles and computation... in rectangles or circles wat ever it may be. the theme of program is to show algorithm
program
program  i want a progra in java to print a sentence in alphabetic order, taking the input from the user.the program should writen without using the array for example : if input= this is a cat then output sould = a cat
 

 

 

DMCA.com