Question

In: Computer Science

Using a while loop. Write a JAVA program that asks a user for an integer between...

Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms.

Sample output:

How many terms would like printed out for the Fibonacci Sequence?

7

Fibonacci Series of 7 numbers:

0 1 1 2 3 5 8

Solutions

Expert Solution

//TestCode.java
import java.util.Scanner;

public class TestCode {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("How many terms would like printed out for the Fibonacci Sequence?");
        int n = scan.nextInt();

        System.out.println("Fibonacci Series of "+n+" numbers:");
        if(n>=1){
            System.out.print(0+" ");
        }
        if(n>=2){
            System.out.print(1+" ");
        }
        if(n>=3) {
            int x = 0, y = 1, z;
            for (int i = 2; i < n; i++) {
                z = x + y;
                x = y;
                y = z;
                System.out.print(z+" ");
            }
            System.out.println();
        }
    }
}


Related Solutions

Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT