Kotlin Program To Calculate The Power Of A Number - Programiz
Learn Python practically and Get Certified.
ENROLLPopular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning PythonPopular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python ExamplesReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View allCreated with over a decade of experience.
- Learn
- Practice
- Compete
Certification Courses
Created with over a decade of experience and thousands of feedback.
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses on Learn C Learn C++ Learn Java Python JavaScript SQL HTML R C C++ Java More languagesLearn Python practically and Get Certified.
Try Programiz PRO!Popular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python TutorialsReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript R C C++ Java KotlinLearn Python practically and Get Certified.
Try Programiz PRO!Popular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python ExamplesKotlin Examples
- Check Whether an Alphabet is Vowel or Consonant
- Find the Largest Among Three Numbers
- Find all Roots of a Quadratic Equation
- Check Leap Year
- Check Whether a Number is Positive or Negative
- Check Whether a Character is Alphabet or Not
- Calculate the Sum of Natural Numbers
- Find Factorial of a Number
Kotlin Tutorials
- calculate the power using recursion
- Calculate Standard Deviation
- Display Armstrong Number Between Two Intervals
- Check Armstrong Number
- Find Factorial of a Number
- Display Armstrong Numbers Between Intervals Using Function
Example 1: Calculate power of a number without using pow()
fun main(args: Array<String>) { val base = 3 var exponent = 4 var result: Long = 1 while (exponent != 0) { result *= base.toLong() --exponent } println("Answer = $result") }When you run the program, the output will be:
Answer = 81In this program, base and exponent are assigned values 3 and 4 respectively.
Using the while loop, we keep on multiplying result by base until exponent becomes zero.
In this case, we multiply result by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81. We also need to cast base to Long because result only accepts Long and Kotlin focuses on type safety.
However, as in Java, above code doesn't work if you have a negative exponent. For that, you need to use pow() function in Kotlin
Here's the equivalent Java code: Java Program to calculate power of a number
Example 2: Calculate power of a number using pow()
fun main(args: Array<String>) { val base = 3 val exponent = -4 val result = Math.pow(base.toDouble(), exponent.toDouble()) println("Answer = $result") }When you run the program, the output will be:
Answer = 0.012345679012345678In this program, we used standard library function Math.pow() to calculate the power of base.
We also need to convert base and exponent to Double because, pow only accepts Double parameters.
Share on: Did you find this article helpful?Sorry about that.
How can we improve it? Feedback * Leave this field blankOur premium learning platform, created with over a decade of experience and thousands of feedbacks.
Learn and improve your coding skills like never before.
Try Programiz PRO- Interactive Courses
- Certificates
- AI Help
- 2000+ Challenges
Related Examples
Kotlin Example
calculate the power using recursion
Kotlin Example
Calculate Standard Deviation
Kotlin Example
Display Armstrong Number Between Two Intervals
Kotlin Example
Check Armstrong Number
Từ khóa » C# 2 Hoch N
-
Math.Pow(Double, Double) Method (System) - Microsoft Docs
-
C# | Math.Pow() Method - GeeksforGeeks
-
Is There An Exponent Operator In C#? - Stack Overflow
-
JavaScript Math Pow() Method - W3Schools
-
Exponent In C# | Delft Stack
-
Scripting API: Mathf.Pow - Unity - Manual
-
Hochzahlen In C#
-
Wie Schreibe Ich 10^-3? - Narkive
-
Potenzbutton Mit C#? Potenzen Rechnen Mit C - Gutefrage
-
Cách Tính Giai Thừa Của Một Số Trong C#
-
Vòng Lặp For Trong C#. | How Kteam
-
SQL POWER() Function - W3resource
-
Zweierpotenz (Power Of Two) - TRAIN Your Programmer