Power of a Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Power of a Number */
#include <stdio.h>
int main(){
    long long base, exp, result = 1;
    scanf("%lld%lld", &base, &exp);
    for(long long i=0;i<exp;i++)
        result *= base;
    printf("%lld", result);
    return 0;
}

C Output

Input:  
2 5 

Output:  
32


C++ Program

// C++ - Power of a Number
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long base, exp, result = 1;
    cin >> base >> exp;
    for(long long i=0;i<exp;i++)
        result *= base;
    cout << result;
}

C++ Output

Input:  
3 4 

Output:  
81


JAVA Program

// Java - Power of a Number
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long base = sc.nextLong(), exp = sc.nextLong(), result = 1;
    for(long i=0;i<exp;i++)
        result *= base;
    System.out.print(result);
  }
}

JAVA Output

Input:  
5 3  

Output:  
125


Python Program

# Python - Power of a Number
base, exp = map(int, input().split())
result = 1
for _ in range(exp):
    result *= base
print(result)

Python Output

Input:  
7 2

Output:  
49


In-Depth Learning – Entire Concept in Paragraphs
Example
For the input 2 5, the output is 32 since 2 multiplied by itself five times (2 × 2 × 2 × 2 × 2) equals 32. The loop continually multiplies the base repeatedly just as the exponentiation definition presents.

Real-Life Analogy
Think of it like stacking boxes where each new layer is the same number of boxes as the entire stack beneath it. If the base is 2 and the exponent is 5, it’s like doubling the number of boxes five times — the pile grows very quickly. This rapid growth is exactly why powers are so important in computing, finance (compound interest), and science.

Why It Matters
Exponentiation is ubiquitous in computer science — from estimating time complexity in algorithms (O(2^n)), to cryptography (large exponents mod a number), to computer graphics (scale transformations). In mathematics, powers form the basis for polynomial equations, exponential growth models, and logarithms.

Learning Insights
This exercise educates on loop repetition and makes one appreciate how exponentiation could be done without using the built-in pow() function. It solidifies that exponentiation is simply multiplying repeatedly and introduces considering optimizations such as fast exponentiation (exponentiation by squaring), which lowers the time complexity from O(exp) to O(log exp).

Interview Relevance and Practical Use
Interviewers may use this as a warm-up to try loop logic and optimization sensitivity. One common twist is to use fast power with modulo arithmetic (employed in modular exponentiation in cryptography). In actual projects, calculations of power are utilized in physics simulations, interest computation, AI learning rate adjustment, and others.

Performance and Edge Notes
The loop-based solution here takes O(exp) time and O(1) space. It's good enough for small exponents, but big ones overflow typical integer types rapidly. With large numbers, use libraries supporting big integers or use modulo arithmetic to make results tractable. For extremely large exponents, use fast exponentiation to speed up.
This number power program in C, C++, Java, and Python computes base raised to the power of an exponent using no built-in functions. It's perfect for novices learning loops, arithmetic operations, and exponentiation basics. The explanation touches on real-life metaphors, the significance of exponentiation in mathematics and computer science, and how to use fast power methods for large exponents. Great for practice programming, competitive coding, and learning growth patterns in algorithms.