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



Home Answers Viewqa Java-Beginners Fibonacci program

Have Programming Question? Ask it here!
 
 
 


j
Fibonacci program
1 Answer(s)      3 years and 5 months 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  program of jdbc using ms access for creating table
program
program  write a javascript program to create a application form with validation
program
program  write a html program in show table and this word in box
program
program  write a program different between to dates in terms of days in java
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  write a program reversee a string. input-hi this is roseindia output-roseindia is this hi
program
program  develop a servlet to insert the data in the database from our program in the table stored in the database
program
program  WAP a java program to form 1/2+3/4+5/6+7/8 series
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  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
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  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 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  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  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  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  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  . Develop a program that accepts an initial amount of money (called the principal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional
program
program  Write a JSP Program, which displays a web page containing the name of the Courses, Courses being offered currently, number of students enrolled in each course, new courses to be offered, eligibility criteria for taking
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
program
program  Utopias tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Define the program calculateTax which determines the tax on the gross pay. Define
program
program  Write a JSP Program, which displays a web page containing two web links one for your profiling and other for the schedule of theory of practical classes of your Batch. When one click on link for getting your profile
program
program  Write a program to print details of a week for a particular day using array of objects Integer and String. OUTPUT: First Day of Week is Monday Second Day of Week is Tuesday Third Day of Week is Wednesday Forth Day
A basic program
A basic program  Write a program that will print out the first 10 numbers, their squares and their cubes. Write a program that displays the first... a program that displays the first 20 powers of 2. Number and then the power
Need to develop Java Program - Development process
Need to develop Java Program  Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some... fibonacci numbers and writes them to another pipe. The main thread should read
Java Program - Java Beginners
Java Program  The numbers in the following sequence are called the fibonacci numbers . 0 , 1 , 1 , 2, 3 , 5 , 8 , 13 , ????.. Write a program using do?..while loop to calculate and print the first m Fibonacci numbers
java program - Java Beginners
for "clear".after pressing show button a fibonacci series will appear where each element of fibonacci series will appear 1fter 10 seconds.after pressing clear it will b cleared.the program should b done onlu using java applet but not with swing
i have problem in that program - JavaMail
program to print all numbers below 100,000 that are both prime and fibonacci number... that generates fibonacci numbers and writes them to another pipe. The main thread should... = new MyThread2(pr2, pw2); System.out.println("Fibonacci Numbers
multi-threaded Java program - Java Beginners
multi-threaded Java program  Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some... fibonacci numbers and writes them to another pipe. The main thread should read
program calculateCylinderVolume
program calculateCylinderVolume  program calculateCylinderVolume
java program for
java program for   java program for printing documents,images and cards
java program - Java Beginners
java program  I have to make two radio buttons where one to show and one for clear.when u ser choose show button a fibonacci series will appear and on choosing clear the page will b cleared.how can i implement this?i cannot take
program in java
program in java  a program to develop a client, server application to satisfy the requests from multiple clients
program in java
program in java  a program to develop a client, server application to satisfy the requests from multiple clients
string program
string program  write a program to print the pattern p r o program r a m
program of c
program of c  write a program in c to display a to z without using printf or scanf statements or header files
program for factorial
program for factorial  how to set size of buttons on frame
 

 

 

DMCA.com