Question

* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...

* Write a Java program that calculates and displays the Fibonacci numbers
* First prompt the user to input a number, N
* Then use a for loop to calculate and display the first N fibonocci numbers
* For example, if the user enters 5, the output should be:
* 1, 1, 2, 3, 5
*
* if the user enters 10, the output should be:
* 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Homework Answers

Answer #1
import java.util.*;
public class Main
{
    public static void main(String args[]) 
    { 
        Scanner s = new Scanner(System.in);
        int n;
        n=s.nextInt(); //Taking the input
        int first = 1, second = 1;  //Since first 2 terms are 1 and 1
        //For loop
        for(int i=0;i<n;i++){
            System.out.print(first + " "); 
            int temp = first + second; //next term in the sequence is sum of last two
            first = second;  //For next step 2nd term is first term 
            second = temp;   //For next step the next term is the second term
        }
    } 
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store...
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store the numbers in an array. The c++ program asks the user for an input to enter a positive integer 'n' and display the nth fibonacci number "FibN" that is stored inside of the array
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
Write a Java program that gets a 4x4 array of numbers and that calculates the sum...
Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner. Examples of resulting programs are: Enter a 4x4 matrix row by row: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 The sum of numbers in the diagonal is: 16 The average of numbers in...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5,...
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. Write a function int fib(int n) that returns Fn. For example, if n = 0, then fib() should return 0, PROGRAM: C
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail. Example of the expected output is as follows:
Write a VSC (macro) program that computes and displays a Fibonacci sequence. A Fbonacci sequence is...
Write a VSC (macro) program that computes and displays a Fibonacci sequence. A Fbonacci sequence is generated by adding the two most recent sequence numbers together, i.e., 1, 1, 1+1-2, 1+2=3, 2+3=5, 3+5=8, … The user will enter the number of terms in the sequence to be displayed. Assemble this program using the VSC assembler (ASM), and simulate this program using the VSC simulator (SIM). Include a copy of the source listing (SOURCE.DAT), the assembled listing (SLIST.DAT) and the simulation...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • When we observe two charged balloons dangling from thin thread, they can repel each other. We...
    asked 7 minutes ago
  • I study management information systems in my fourth year, but I do not know anything about...
    asked 7 minutes ago
  • What features of the environment are people “wired” to detect? Why? Would it be adaptive for...
    asked 8 minutes ago
  • Graduates of fashion marketing group men 8 women 20 have chosen work in France, probability of...
    asked 19 minutes ago
  • Despite guidelines regulating eligibility for special education services, today’s special education classrooms continue to be over   populated...
    asked 41 minutes ago
  • A. Find the sum 3+8+13+...+28 B.  Find the first term and the common difference of the arithmetic...
    asked 44 minutes ago
  • Explain why in a multiplier model a rise in household wealth relative to target will ceteris...
    asked 1 hour ago
  • What political and economic roles have Japan and the United States played in Southeast Asia in...
    asked 1 hour ago
  • What does the function split do? Explain with an example. ​ ​ def split(L, first): return...
    asked 1 hour ago
  • Code a simple Editor class with 2 constructors (one default, one with a string for a...
    asked 1 hour ago
  • Using a greedy algorithm solve the following instance of Interval Scheduling {(2,12),(1,7),(3,5),(8,10),(7,14),(9,16),(10,14),(12,13),(17,22),(13,14),(15,20),(14,18),(20,30),(22,25),(26,27),(25,29),(24,31)}
    asked 1 hour ago
  • * Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
    asked 1 hour ago