• Register
1 vote


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

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


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: ");
  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);
  System.out.print("Please enter a number: ");
  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;



I hope this will help you to understand better. Keep coding  cool

13 9 6
94,240 points

Related questions

0 votes
1 answer 8 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 40 views
2 votes
1 answer 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.
asked Jun 3, 2020 Gavin 15.3k points
2 votes
1 answer 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’t have any working code. “C program to find square root of a number without using sqrt function”. Can somebody help me in answering above question?
asked Mar 24, 2020 morrisBson 3.2k points
0 votes
1 answer 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: “This is an assignment”. Thanks!
asked Jan 6 Code Learner 5.5k points
0 votes
1 answer 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.
asked Nov 6, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 23 views
Problem: How to convert decimal to binary, Java has a method “Integer. toBinaryString()”. The method returns a string representation of the integer argument as an unsigned integer in base 2.N
asked Nov 5, 2020 Mashhoodch 5.7k points
0 votes
1 answer 19 views
Problem: How to reverse a number in java. I need it by using recursion. Please suggest any solution.
asked Nov 3, 2020 Mubashi-Question 1.7k points
2 votes
1 answer 12 views
Problem: Hello all, I am a secondary school level student and recently started learning Java programming out my curiosity. I have got a textbook on Java programming. I just completed the chapter of for-loop and continue solving the exercises of this chapter. I have got weird a ... you guys. But, yeah it is important to know for me. Thanks for being nice to me. I must recognize your help. Thanks.
asked Apr 12, 2020 Gavin 15.3k points
1 vote
1 answer 20 views
Problem: Folks, I hope you are doing well. I started learning java programming very recently. I am on the chapter of Data Type right now of the book from where I am taking the help. There is a problem give in my book’s exercise that: “if you want to ... . Is it really possible to do so? If so, then how? Could you please give a hint regarding the problem given above? Best regards.
asked Mar 17, 2020 Gavin 15.3k points