How To Raise a Number To the Second Power in Java
This is one of the articles from our Java Tutorial for Beginners.
Your Task:
To write a method, raising a number to the second power. For example:
Then you have to add the number 2 to the result.
For example:
- if a user types in the number 2, then the numbers 4 and 6 have to be displayed in the console
- if a user types in the number 3, then the numbers 9 and 11 have to be displayed in the console
- if a user types in the number 5, then the numbers 25 and 27 have to be displayed in the console
- etc.
Solution - 1st approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Test{ static int square(int a){ int result = a*a; return result; } public static void main(String[] args){ int a1 = square(5); System.out.println(a1); System.out.println(a1 + 2); } } |
If you run this code on your computer, you will see the following in your console:
25
27
Commentary:
First of all, you have to find out the formula of raising a number to the second power. As you see, the formula will look like this a*a:
That's why we wrote a method called square() with the help of these lines of code:
1 2 3 |
static int square(int a){ int result = a*a; return result; |
And in this line of the code we get a number, that will be raised to the second power. Please note, that the number has to be an integer, as we wrote in the code int a:
1 |
static int square(int a) |
Then to multiply the number by itself, we wrote this line:
1 |
int result = a*a; |
And then we call the method square():
1 2 3 4 5 6 |
public static void main(String[] args){ int a1 = square(5); System.out.println(a1); System.out.println(a1 + 2); } |
As you can see, in this case we raise the number 5 to the second power. As a result, we get 25 and we assign 25 to the variable called a1.
1 |
int a1 = square(5); |
Then with the help of
1 |
System.out.println(a1); |
the number 25 will be displayed in the console.
And then with the help of
1 |
System.out.println(a1 + 2); |
the number 27 wil be displayed in the console.
Solution - the 2nd approach
1 2 3 4 5 6 7 8 9 10 |
public class Test{ static int square(int a){ return a*a; } public static void main(String[] args){ System.out.println( square(5) ); System.out.println( square(5) + 2 ); } } |
Commentary:
In this approach, unlike the 1st approach described above, this part of code
1 2 3 4 |
static int square(int a){ int result = a*a; return result; } |
has been replaced with
1 2 3 |
static int square(int a){ return a*a; } |
So the formula of raising a number to the second power (a*a) has been written next to "return".
And these lines of code from the 1st approach
1 2 3 4 5 6 |
public static void main(String[] args){ int a1 = square(5); System.out.println(a1); System.out.println(a1 + 2); } |
have been replaced with
1 2 3 4 |
public static void main(String[] args){ System.out.println( square(5) ); System.out.println( square(5) + 2 ); } |
Thefore, we've got fewer lines of code. And still it is an easy-to-read code.
You can find more articles in our Java Tutorial for Beginners.