Texts: 1.37 Basic output with variables (Java)
This zyLab activity is intended for students to prepare for a larger programming assignment. Warm-up exercises are typically simpler and self-practice. The last section provides a full programming assignment.
A variable like userNum can store a value like an integer. Extend the given program to print userNum values as indicated. (Submit for 2 points).
(1) Output the user's input.
Enter integer: 4
You 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: 4
You entered: 4
4 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: 4
You entered: 4
4 squared is 16
And 4 cubed is 64!!
Enter another integer: 5
4 + 5 is 9
4 * 5 is 20
511912.3344536.qx3zoy7
My library > IT 145: Intro to Software Development home > 1.37: Basic output with variables (Java)
zyBooks catalog Help/FAQ
OutputWithVars.java
Load 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();
System.out.println("You entered: " + userNum);
System.out.println(userNum + " squared is " + (userNum * userNum));
System.out.println("And " + userNum + " cubed is " + (userNum * userNum * userNum));
System.out.println("Enter another integer:");
int userNum2 = scnr.nextInt();
System.out.println(userNum + " + " + userNum2 + " is " + (userNum + userNum2));
System.out.println(userNum + " * " + userNum2 + " is " + (userNum * userNum2));
}
}
Submit mode
Run your program as often as you'd like, before submitting for grading. Below, type any needed second box.
Enter program input (optional)
If your code requires input values, provide them here.
Your program expects input
Run program
Input (from above)
OutputWithVars.java (Your program)
Output (shown below)