Question

Prompt the user for a positive whole number (call it n) Use a loop to calculate...

Prompt the user for a positive whole number (call it n)

Use a loop to calculate the following:

Sum: 1+2+...+n

Sum of squares: 1^2 + 2^2 + ... + n^2

factorial: 1 x 2 x ... x n

Print out the numbers when the loop is done.

Do this with a for loop and a while loop.

Call the class LoopArithmetic

Sample output:

The sum from 1 up to 5 is: 15

The sum of squares 1 to 5 is: 55

The factorial of 5 is: 120

Provide output for at least 3 different numbers.

Homework Answers

Answer #1
import java.util.Scanner;

public class LoopArithmetic {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = in.nextInt();
        int sum = 0, sumSquares = 0, factorial = 1, j = 1;
        for (int i = 1; i <= n; i++) {
            sum += i;
            sumSquares += i * i;
        }
        while (j <= n) {
            factorial *= j;
            j++;
        }
        System.out.println("The sum from 1 up to " + n + " is: " + sum);
        System.out.println("The sum of squares 1 to " + n + " is: " + sumSquares);
        System.out.println("The factorial of " + n + " is: " + factorial);
    }
}

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
Assembler lanaguage program   The program is to prompt the user to enter a number between 2...
Assembler lanaguage program   The program is to prompt the user to enter a number between 2 and 100. Reject any invalid user inputs and terminate the program if invalid inputs are entered. Print each even number from 2 to the user entered number. Sum these even numbers and print the sum. Print each odd number from 1 to the user entered number. Sum these odd numbers and print the sum. Exit the program when the output is complete. The output...
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.
* 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
1) In Python Use a while loop to ask the user to enter a series of...
1) In Python Use a while loop to ask the user to enter a series of alphabets. The loop terminates when the user presses enter. Once the loop terminates, display the number of vowels and consonants entered by the user. Hint: keep a running count of the number of characters entered by the user. Keep a running count of the number of vowels. Total number of consonants = total number of characters - total number of vowels. Assume that the...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
C - Language Write an expression that executes the loop while the user enters a number...
C - Language Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...
Create a C++ Program that prompts the user to enter a number and compute for the...
Create a C++ Program that prompts the user to enter a number and compute for the factorial value. SAMPLE OUTPUT: ENTER A NUMBER: -5 zero & positive numbers only!!! ENTER A NUMBER: 5 Factorial of 5 : 5*4*3*2*1 = 120 DO NOT USE FUNCTION FOR THIS PROBLEM. THANK YOU   
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Use a while loop (and brute force) to identify the first positive integer whose natural log...
Use a while loop (and brute force) to identify the first positive integer whose natural log is larger than 10. Outside the loop, set a numeric variable (call it start) equal to 1. Inside the loop, print the value of log(start), and increase the value of start by 1. DO NOT submit the entirety of the output that is printed to the console. Instead, provide your code and the value of start that causes the loop to terminate. Using Rstudio!!!
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...