
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Calculate the Power of a Number in Java Program
In this article, we will understand how to calculate the power of a number. The power of a number is calculated using a loop and multiplying it by itself multiple times.
Below is a demonstration of the same −
Input
Suppose our input is −
Number : 4 Exponent value : 5
Output
The desired output would be the following i.e. 45
The result is 1024
Algorithm
Step 1 - START Step 2 – Declare two integer values namely my_input and my_exponent Step 3 - Read the required values from the user/ define the values Step 4 – Using a while loop, multiply the input value with itself for n number of times where n is the exponent value. Store the result. Step 5- Display the result Step 6- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class Exponents { public static void main(String[] args) { int my_input , my_exponent; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); System.out.print("Enter the exponent value : "); my_exponent = my_scanner.nextInt(); long my_result; my_result = 1; while (my_exponent != 0) { my_result *= my_input; --my_exponent; } System.out.println("The result is = " + my_result); } }
Output
Required packages have been imported A reader object has been defined Enter the number : 4 Enter the exponent value : 5 The result is = 1024
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Exponents { public static void main(String[] args) { int my_input , my_exponent; my_input = 4; my_exponent = 5; System.out.println("The number is defined as " +my_input +" and the exponent is defined as " + my_exponent); long my_result; my_result = 1; while (my_exponent != 0) { my_result *= my_input; --my_exponent; } System.out.println("The result is = " + my_result); } }
Output
The number is defined as 4 and the exponent is defined as 5 The result is = 1024
- Related Articles
- Java program to calculate the power of a number
- Java program to calculate the power of a Given number using recursion
- Swift Program to Calculate the Power of a Number
- Haskell Program to Calculate the Power of a Number
- Kotlin Program to Calculate the Power of a Number
- C++ Program to Calculate Power of a Number
- C program to calculate power of a given number
- Java Program to calculate the power using recursion
- Calculate the power on a BigInteger in Java
- How to calculate Power of a number using recursion in C#?
- Java program to calculate the GCD of a given number using recursion
- Java program to calculate the factorial of a given number using while loop
- Haskell Program to calculate the power using Recursion
- Golang Program to Calculate The Power using Recursion
- C++ Program to Calculate Power Using Recursion

Advertisements