In: Computer Science
Output with Vars.java
A variable like userNum can store a value like an integer. Extend the given program as indicated.
Note: This zyLab outputs a newline after each user-input
prompt. For convenience in the examples below, the user's input
value is shown on the next line, but such values don't actually
appear as output when the program runs.
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
import java.util.Scanner;
public class OutputWithVars {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userNum;
System.out.println("Enter integer:");
userNum = scnr.nextInt();
}
}
//OutputWithVars.java import java.util.Scanner; public class OutputWithVars { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userNum; System.out.println("Enter integer:"); userNum = scnr.nextInt(); System.out.println("you entered: "+userNum);//(1) task completed. int s=userNum * userNum; System.out.println(userNum+" square is "+s); int c=userNum * userNum * userNum; System.out.println("And "+userNum+ " cube is "+c+"!!");//(2) task completed. int userNum2=0; //declaration and initialization of another varaible System.out.println("Enter another integer:"); userNum2 = scnr.nextInt(); //reading input from the keyboard by using scanner objecct. int a =userNum+userNum2; int m=userNum*userNum2; System.out.println(+userNum+ " + " +userNum2 +" is " +a); System.out.println(+userNum+ " * " +userNum2 +" is " +m); } }