Multiply the number by itselfCall the Math.pow function

The adhering to examples show these philosophies.

You are watching: How to square a number in java

1) Square a number by multiplying it by itself

This is exactly how you square a number by multiplying it by itself:

int i = 2;int square = i * i;In this instance if you print the value of square, it will be 4.

2) Square a number via the Math.pow method

Here’s how you contact the Math.pow technique to square a number:

int i = 2;int square = Math.pow(i, 2);

More power multipliers

In general I just multiply the number by itself to get the squared worth, yet the advantage of the Math.pow strategy is that when you understand exactly how to use it, you have the right to quickly cube a number prefer this:

int i = 2;int square = Math.pow(i, 3);Thus, it’s valuable to understand both viewpoints.

See more: How To Insert Micro Sd Card In Samsung Galaxy Tab A, Sd Card: Samsung Galaxy Tab A

In summary, if you wanted to see a couple of ways to square a number (int, float, double, and so on.) in Java, I hope this is valuable.


publications i’ve written


front page alvin on twitter search privacy terms & problems is owned and operated by Valley Programming, LLC In regards to links to, As an Amazon Associate I (Valley Programming, LLC) earn from qualifying purchases This website provides cookies: learn more
java java applets java faqs misc content java resource code test projects lejos Perl perl faqs programs perl recipes perl tutorials
Unix man (help) pperiods unix by example tutorials resource code warehome java examples drupal examples
misc privacy policy terms & problems subscribe unsubscribe wincvs tutorial feature point analysis (fpa) fpa tutorial
Other call me rss feed my photos life in alaska how i sold my service living in talkeetna, alaska my bookmarks inspirational quotes resource code snippets
This website supplies cookies: learn even more is owned and also operated by Valley Programming, LLC In regards to links to, “As an Amazon Associate I (Valley Programming) earn from qualifying purchases”