294,390 views
37 votes
37 votes
A variable like userNum can store a value like an integer. Extend the given program to print userNum values as indicated.(1) Output the user's input.Enter integer: 4You entered: 4(2) Extend to output the input squared and cubed. Hint: Compute squared as userNum * userNum. (Submit for 2 points, so 4 points total).Enter integer: 4You entered: 44 squared is 16 And 4 cubed is 64!! (3) Extend to get a second user input into userNum2. Output sum and product. (Submit for 1 point, so 5 points total).Enter integer: 4You entered: 44 squared is 16 And 4 cubed is 64!!Enter another integer: 54 + 5 is 94 * 5 is 20LABACTIVITY1.16.1: Basic output with variables (Java)0 / 5OutputWithVars.javaLoad default template...import java.util.Scanner;public class OutputWithVars {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);int userNum = 0;System.out.println("Enter integer: ");userNum = scnr.nextInt(); return;}}import java.util.Scanner;public class OutputWithVars {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);int userNum = 0;System.out.println("Enter integer: ");userNum = scnr.nextInt();return;}}Develop modeSubmit modeRun your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box.

User SqueekyDave
by
3.1k points

1 Answer

13 votes
13 votes

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

int userNum;

Scanner input = new Scanner(System.in);

System.out.print("Enter integer: ");

userNum = input.nextInt();

System.out.println("You entered "+userNum);

System.out.println(userNum+" squared is "+(userNum*userNum));

System.out.println(userNum+" cubed is "+(userNum*userNum*userNum));

int userNum2;

System.out.print("Enter another integer: ");

userNum2 = input.nextInt();

int sum= userNum + userNum2; int product = userNum2 * userNum;

System.out.println(userNum+" + "+userNum2+" = "+sum);

System.out.println(userNum+" * "+userNum2+" = "+product); } }

Step-by-step explanation:

This declares userNum

int userNum;

Scanner input = new Scanner(System.in);

This prompts the user for input

System.out.print("Enter integer: ");

This gets user input from the user

userNum = input.nextInt();

Number (1) is implemented here

System.out.println("You entered "+userNum);

Number (2) is implemented here

System.out.println(userNum+" squared is "+(userNum*userNum));

System.out.println(userNum+" cubed is "+(userNum*userNum*userNum));

This declares another variable userNum2

int userNum2;

This prompts the user for another input

System.out.print("Enter another integer: ");

This gets user input from the user

userNum2 = input.nextInt();

This calculates the sum and the product

int sum= userNum + userNum2; int product = userNum2 * userNum;

Number (3) is implemented here

System.out.println(userNum+" + "+userNum2+" = "+sum);

System.out.println(userNum+" * "+userNum2+" = "+product);

User Leandro Soares
by
3.3k points