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.

Code Logic

  1. Input:
    • The variables num and power are assigned the values 3 and 2 respectively. This means we want to calculate the power of 3 raised to the 2nd power.
  2. Initialization:
    • The variable output is initialized to 1. This variable will store the result of raising num to the power of power.
  3. Loop to Calculate Power:
    • A for loop is used to iterate power times. In this case, the loop iterates 2 times because power is 2.
    • In each iteration, the value of num is multiplied to output. This is equivalent to raising num to the power of power.
    • After the loop completes, output will hold the result of raising num to the power of power.
  4. Output:
    • The final result stored in output is printed, which represents the power of num raised to the power.
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