Question

Using Java: While and for loop to generate numbers in the Fibonacci sequence Tasks: 1. Using...

Using Java: While and for loop to generate numbers in the Fibonacci sequence

Tasks:

1. Using your understanding of Fibonacci numbers chalk out how you would use a while loop to generate (and display) numbers in the Fibonacci sequence. The program that you are attempting to write must take a number, n, from the user that tells you how many Fibonacci numbers to display. Write the while loop portion of how you think this would work below. (You do not need to show taking the number n from the user.)

2. Now write the program that takes the number, n, from the user and displays that many numbers in the Fibonacci series.

3. Now rewrite the program using a For loop. What changes are required to make this happen?

Homework Answers

Answer #1

Answer 1:

       System.out.print(f + " " + s);// printing 0 and 1

while (i<n) {
           //adding first and second and putting in 3rd
           t = f + s;
           //printing 3rd
           System.out.print(" " + t);
           //swapping values
           f = s;
           s = t;
           i++;
       }

Answer 2:

import java.util.Scanner;

public class FixExample {
   public static void main(String args[]) {
       int f = 0, s = 1, t, i,n;
      
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter how many number to display");
       n=sc.nextInt();
       System.out.print(f + " " + s);// printing 0 and 1
       i = 2; // starting from 2 as 0,1 is printed
       while (i<n) {
           //adding first and second and putting in 3rd
           t = f + s;
           //printing 3rd
           System.out.print(" " + t);
           //swapping values
           f = s;
           s = t;
           i++;
       }

   }
}

Answer 3:

//Just we need to change the for loop in place of while. everything will be same

import java.util.Scanner;

public class FixExample {
   public static void main(String args[]) {
       int f = 0, s = 1, t, i,n;
      
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter how many number to display");
       n=sc.nextInt();
       System.out.print(f + " " + s);// printing 0 and 1
       // starting from 2 as 0,1 is printed
       for (i = 2;i<n;i++) {
           //adding first and second and putting in 3rd
           t = f + s;
           //printing 3rd
           System.out.print(" " + t);
           //swapping values
           f = s;
           s = t;
       }

   }
}

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
* 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
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 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
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
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...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display 5 lines of smiley faces. The following would be the output of your program: Note: Sample program is in the "Important Documents", Module 6 folder) Hello:                          Bye!
This is an intro to Java question. Please solve using pseudocode if you can. Fibonacci ​...
This is an intro to Java question. Please solve using pseudocode if you can. 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: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Hence, a Fibonacci number ​n ​ may be found with ​f(n) = f(n-1)...
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...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
JAVA Prompt the user for 10 numbers using a while loop. Create an int variable number....
JAVA Prompt the user for 10 numbers using a while loop. Create an int variable number. You have three additional int variables pos, neg, and zero. Your goal is to determine the number of positive, negative, and zeroes in the list of the ten numbers read in.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT