BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Calculate Power of a Number

Last Updated: September 12, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this article, we will write java programs to calculate power of a number.

1. Program to calculate power of a number using for loop

In this program we are calculating the power of a given number using for loop. Here number is the base and p is the power (exponent). So we are calculating the result of number^p.

public class JavaExample {
    public static void main(String[] args) {
    	//Here number is the base and p is the exponent
        int number = 2, p = 5;
        long result = 1;
        
        //Copying the exponent value to the loop counter
        int i = p;
        for (;i != 0; --i)
        {
            result *= number;
        }
        
        //Displaying the output
        System.out.println(number+"^"+p+" = "+result);
    }
}

Output:
Java Program to calculate power of a number using for loop

2. Program to calculate power of a number using while loop

Here we are writing the same program that we have seen above using while loop.

public class JavaExample {
    public static void main(String[] args) {
        int number = 5, p = 2;
        long result = 1;
        
        int i=p;
        while (i != 0)
        {
            result *= number;
            --i;
        }
        System.out.println(number+"^"+p+" = "+result);
    }
}

Output:
Java Program to calculate power of a number using while loop

3. Program to calculate power of a number using pow() function

In the above two programs, we have seen how to calculate power of a number using loops. In the following program, we are using pow() function to calculate power.

public class JavaExample {
    public static void main(String[] args) {
    	int number = 10, p = 3;
        double result = Math.pow(number, p);
        System.out.println(number+"^"+p+" = "+result);
    }
}

Output:
Program to calculate power of a number using pow() function

Related Java Examples:

1. Java program to find quotient and remainder
2. Java program to calculate simple interest
3. Java program to calculate compound interest
4. Java program to print Pascal’s Triangle

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Fascinating Number Program in Java
  3. Sunny Number Program in Java
  4. Java program to reverse a number using for, while and recursion
  5. Java Program to Calculate Compound Interest

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap