# How to square a number in java

1 vote
20 views

## Problem:

I am new to the programming world. Just started learning with Java. I know my question is the very basic level. But, yeah I do want to know, how can I square a number in Java?

Best regards

1 vote

## Solution:

Welcome mate.

Let’s come to the point. You can square a number in a couple of ways. The easiest way to square a number is multiplying with itself.  Another way is to call a method named math.pow

Let’s see how to do it using math.pow

import java.util.*;
public class Square{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
int number;
System.out.print("Enter a number: ");
num=sc.nextInt();
System.out.println("Square of "+ number + " is: "+ Math.pow(number, 2));

}

}

Using a method where we will multiply the number with itself

import java.util.Scanner;
public class SquareNum{

public static void main(String [] args){

Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("Your squared number is: " + square(num));

}

//Here is our method to get the square

public static int square(int num){

return num * num;

}

}

## Related questions

8 views
I need to check if a number is a perfect squre in java.
40 views
26 views
Problem: Hello sir, I was studying the python language and I started implementing the program I can do it by the simple multiplying by itself but this looks l very simple is there is another way in which we can achieve this..? Below is my simple solution. x = input("Enter the number") num = x * x print("Square number is",num) I hope another way to solve the problem will get as soon as possible.
425 views
Problem : I have few years of experience in working with C programming. Now I am preparing for the interview and while preparing for it I came to below question for which I don&rsquo;t have any working code. &ldquo;C program to find square root of a number without using sqrt function&rdquo;. Can somebody help me in answering above question?
9 views
Problem: Hello Kodlogs! I am a beginner and started learning programming through an online course. In course, I am given an assignment in which I have to write a program that will count the occurrence of each character in a string and then print the number of ... with detailed comments so that I can understand the logic and program? The given string is: &ldquo;This is an assignment&rdquo;. Thanks!
105 views
Problem: Hi dear friends is there any way to find out the sum of total element of array which is equal to given number. Please suggest any example.
23 views
Problem: How to convert decimal to binary, Java has a method &ldquo;Integer. toBinaryString()&rdquo;. The method returns a string representation of the integer argument as an unsigned integer in base 2.N