Program to check the Power of Number
Definition & Explanation
More Description
Program Logic
C
Method 1 :
#include <stdio.h>
double power(double x, int n) {
double result = 1.0;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
int main() {
double x = 2.0;
int n = 3;
printf("%.2f raised to the power of %d is %.2f\n", x, n, power(x, n));
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
using namespace std;
double power(double x, int n) {
double result = 1.0;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
int main() {
double x = 2.0;
int n = 3;
cout << x << " raised to the power of " << n << " is " << power(x, n) << endl;
return 0;
}
Output :
JAVA
Method 1 :
public class Main {
static double power(double x, int n) {
double result = 1.0;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
public static void main(String[] args) {
double x = 2.0;
int n = 3;
System.out.println(x + " raised to the power of " + n + " is " + power(x, n));
}
}
Output :
Python
Method 1 :
def power(x, n):
result = 1.0
for i in range(n):
result *= x
return result
x = 2.0
n = 3
print(f"{x} raised to the power of {n} is {power(x, n)}")