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: 4 You entered: 4

(2) Extend to output the input squared and cubed. 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. 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.

Respuesta :

Answer:

This program is written using Java programming language.

No comments were used; however, see explanation section for line by line explanation

import java.util.*;

public class Nums {

   public static void main(String args[]) {

     Scanner input = new Scanner(System.in);

     System.out.println("1.");

     int userNum;

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

     userNum = input.nextInt();

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

     

     System.out.println("2.");

     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("And "+userNum+" cubed is "+(userNum * userNum * userNum)+"!!");

     

     System.out.println("3.");

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

     int userNum2 = input.nextInt();

     System.out.println(userNum+" + "+userNum2+" is "+(userNum + userNum2));

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

     

   }

}

Explanation:

This enables the program accept inputs

     Scanner input = new Scanner(System.in);

This signifies the beginning of number 1

     System.out.println("1.");

Variable userNum is declared as type integer

     int userNum;

The line prompts the user for input

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

The line accepts the input

     userNum = input.nextInt();

This line displays user input

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

     

This signifies the beginning of number 2

     System.out.println("2.");

This line prompts the user for input

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

This line accepts input

     userNum = input.nextInt();

This line prints user input (as required in number 2)

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

This line calculates and prints the square of user input

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

This line calculates and prints the cube of user input

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

     

This signifies the beginning of number 3

     System.out.println("3.");

This line prompts the user for another integer value

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

This line accepts the input from the user

     int userNum2 = input.nextInt();

This line adds the two inputs by the user and displays the result

     System.out.println(userNum+" + "+userNum2+" is "+(userNum + userNum2));

This line multiplies the two inputs by the user and displays the result

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