Skip to main content

Command Palette

Search for a command to run...

Java Programming: Square of a number

Updated
1 min readView as Markdown
Java Programming: Square of a number

Example

Input 
Enter the number 5 
Output 
The squared of the number is 25.0

Required to know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of functions like System. out.print(), Use of Math.Pow(), and Use of Data types.

Steps

  • Take the number as an input from the user.

  • Import the math package for the usage of the pow function. Here the pow function will be used for finding the square of a number.

  • Print the squared value.

Program

import java.util.Scanner;

public class square {

    public static void main(String[] args) {
        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter a number.
        System.out.print("Enter a number: ");
        int number = sc.nextInt();

        // Find the square of the number.
        double power = Math.pow(number,2);

        // Print the square of the number.
        System.out.println("The square of " + number + " is " + power);
    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.