Program to check : Power of the Number
Definition & Explanation
The power of a number refers to the result obtained by raising the number to a specified exponent. For example, if we have a number num
and an exponent power
, then the power of num
raised to the power
can be calculated as num
raised to the power power
.
For instance, if num
is 3 and power
is 2, then the power of 3 raised to the 2nd power is calculated as 32=932=9.
More Description
Code Logic
- Input:
- The variables
num
andpower
are assigned the values 3 and 2 respectively. This means we want to calculate the power of 3 raised to the 2nd power.
- The variables
- Initialization:
- The variable
output
is initialized to 1. This variable will store the result of raisingnum
to the power ofpower
.
- The variable
- Loop to Calculate Power:
- A for loop is used to iterate
power
times. In this case, the loop iterates 2 times becausepower
is 2. - In each iteration, the value of
num
is multiplied tooutput
. This is equivalent to raisingnum
to the power ofpower
. - After the loop completes,
output
will hold the result of raisingnum
to the power ofpower
.
- A for loop is used to iterate
- Output:
- The final result stored in
output
is printed, which represents the power ofnum
raised to thepower
.
- The final result stored in
C
Method 1 :
#include <stdio.h>
int main() {
int num = 3, power = 2; // Number and its power
int output = 1; // Initialize output to 1
for (int i = 0; i < power; ++i) {
output *= num;
}
printf("%d\n", output); // Output the result
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
using namespace std;
int main() {
int num = 3, power = 2; // Number and its power
int output = 1; // Initialize output to 1
for (int i = 0; i < power; ++i) {
output *= num;
}
cout << output << endl; // Output the result
return 0;
}
Output :
JAVA
Method 1 :
public class Main {
public static void main(String[] args) {
int num = 3, power = 2; // Number and its power
int output = 1; // Initialize output to 1
for (int i = 0; i < power; ++i) {
output *= num;
}
System.out.println(output); // Output the result
}
}
Output :
Python
Method 1 :
num,power=3,2
output=1
for i in range(power):
output*=num
print(output)
Output :
9