Write a Java Program To Input a Number and Display Its Square. The sample run of the required Java program is shown in the image below:
For getting input in a Java program, we may use the Scanner class. Please click on the above heading to read the complete input process step by step with images and the sample Java program.
The Source code of Java Program to input a number and calculate its square
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; /** * * @author H2O */ public class Code4Example { /** * @param args the command line arguments */ public static void main(String[] args) { int num, square; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number to calculate square="); num = keyboard.nextInt(); square = num * num; System.out.print("Square = " + square); } } |
Output:
Which of the following code is to read a number and display its square.
More java code examples click here.