;

Java Program to Calculate the Power of a Number


Tutorialsrack 26/06/2021 Java

In this Java program, you’ll learn how to calculate the power of a number. This post explains various ways to calculate the power of a number with and without using pow() function.

Here are examples to calculate the power of a number in Java.

Example 1: Java Program to Calculate Power of a Number using a While Loop

In this program, you will see how to calculate the power of a number using a while loop.

Example 1: Java Program to Calculate Power of a Number using a While Loop
//Java Program to Calculate power of a number using a While Loop

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		long result = 1;

		Scanner sc = new Scanner(System.in);

		// Base Number - To calculate power of a number
		System.out.println("Enter a Base Number: ");
		int base = sc.nextInt();

		// Power of a Number - no. of times number to be multiplied
		System.out.println("Enter a Exponent: ");
		int exponent = sc.nextInt();
		int power = exponent;
		// closing scanner class(not compulsory, but good programming practice)
		sc.close();

		while (exponent != 0) {
			result *= base;
			--exponent;
		}

		System.out.println("Output of " + base + " to the power " + power + " = " + result);
	}
}
Output

Enter a Base Number: 

5

Enter a Exponent: 

3

Output of 5 to the power 3 = 125

Example 2: Java Program to Calculate Power of a Number using a For Loop

In this program, you will see how to calculate the power of a number using a for loop.

Example 2: Java Program to Calculate Power of a Number using a For Loop
//Java Program to Calculate power of a number using a For Loop

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		long result = 1;

		Scanner sc = new Scanner(System.in);

		// Base Number - To calculate power of a number
		System.out.println("Enter a Base Number: ");
		int base = sc.nextInt();

		// Power of a Number - no. of times number to be multiplied
		System.out.println("Enter a Exponent: ");
		int exponent = sc.nextInt();
		int power = exponent;
		// closing scanner class(not compulsory, but good programming practice)
		sc.close();

		for (; exponent != 0; --exponent) {
			result *= base;
		}

		System.out.println("Output of " + base + " to the power " + power + " = " + result);
	}
}
Output

Enter a Base Number: 

5

Enter a Exponent: 

2

Output of 5 to the power 2 = 25

Example 3: Java Program to Calculate Power of a Number using a pow() Function

In this program, you will see how to calculate the power of a number using a Math.pow() function.

Example 3: Java Program to Calculate Power of a Number using a pow() Function
//Java Program to Calculate Power of a Number using a pow() Function

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		// Base Number - To calculate power of a number
		System.out.println("Enter a Base Number: ");
		int base = sc.nextInt();

		// Power of a Number - no. of times number to be multiplied
		System.out.println("Enter a Exponent: ");
		int exponent = sc.nextInt();
		int power = exponent;
		// closing scanner class(not compulsory, but good programming practice)
		sc.close();

		double result = Math.pow(base, exponent);

		System.out.println("Output of " + base + " to the power " + power + " = " + result);
	}
}
Output

Enter a Base Number: 

3

Enter a Exponent: 

4

Output of 3 to the power 4 = 81.0

Example 4: Java Program to Calculate Power of a Negative Number

In this program, you will see how to calculate the power of a negative number using a Math.pow() function.

Example 4: Java Program to Calculate Power of a Negative Number
//Java Program to Calculate Power of a Negative Number

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		// Base Number - To calculate power of a number
		System.out.println("Enter a Base Number: ");
		int base = sc.nextInt();

		// Power of a Number - no. of times number to be multiplied
		System.out.println("Enter a Exponent: ");
		int exponent = sc.nextInt();
		int power = exponent;
		// closing scanner class(not compulsory, but good programming practice)
		sc.close();

		double result = Math.pow(base, exponent);

		System.out.println("Output of " + base + " to the power " + power + " = " + result);
	}
}
Output

Enter a Base Number: 

-5

Enter a Exponent: 

3

Output of -5 to the power 3 = -125.0


Related Posts



Comments

Recent Posts
Tags