The following Java program was created to take an integer from the user and square it. However, the developer was not familiar with printing outputs. Update the code to print the square of the input number.
Printing data
Square a user-inputted integer
Report a typo
Sample Input 1:
5Sample Output 1:
25Sample Input 2:
-3Sample Output 2:
9Write a program in Java 25
import java.util.Scanner; // essential import for user input
public class Main {
public static void main(String[] args) {
// Create a new scanner for user input
Scanner sc = new Scanner(System.in);
// Taking integer input from the user
int number = sc.nextInt();
// Calculating the square of the input
int squared_number = number * number;
// TODO: Print the result out
// Important to close the scanner!
sc.close();
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.