Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A variable like userNum can store a value like an integer. Extend the given prog

ID: 3883224 • Letter: A

Question

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.

(2) Extend to output the input squared and cubed. Hint: Compute squared as userNum * userNum. .

(3) Extend to get a second user input into userNum2. Output sum and product.

using the following template and in Java:

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;
   }
}

Explanation / Answer


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); // 1, 2, 3
      int square = userNum*userNum; // 2, 3
      System.out.println(userNum + " squared is " + square); //2, 3
      int cube = square*userNum; //2, 3
      System.out.println("And " + userNum + " cubed is " + cube); //2, 3
       System.out.println(userNum + " + 5 = " + (userNum + 5));//3
       System.out.println(userNum + " * 5 = " + (userNum * 5));//3
    
      return;
   }
}

NOTE: Numbers in the comment indicates that line bengos to that particular answer. eg.
           int cube = square*userNum; //2, 3
indicates that this line is answer to question 2 and question 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote