C Program to Find Power of a Number

This C program calculates the power of a given number raised to a specified exponent using a loop.

Problem Statement

Given a base number base and an exponent exponent, we need to calculate the result of raising the base to the power of exponent. The program should use a loop to perform the calculation and display the result.

C Program to Find Power of a Number

#include <stdio.h>

int power(int base, int exponent);

int main() {
    int base, exponent, result;

    // Introduction
    printf("Power Calculation Program\n");

    // Problem Statement
    printf("Enter the base: ");
    scanf("%d", &base);
    printf("Enter the exponent: ");
    scanf("%d", &exponent);

    // Program
    result = power(base, exponent);

    // Output
    printf("%d raised to the power of %d is %d\n", base, exponent, result);

    return 0;
}

int power(int base, int exponent) {
    int result = 1;

    // Calculate power
    while (exponent > 0) {
        result *= base;
        exponent--;
    }

    return result;
}

How it works

  • The program starts with an introduction to the Power Calculation Program.
  • It then prompts the user to enter the base and exponent.
  • The power function is defined, which takes two integers as arguments: base and exponent.
  • Inside the power function, a variable result is initialized to 1.
  • The function uses a while loop to calculate the power by multiplying the base with itself exponent number of times.
  • After the power function is called and the result is obtained, it is displayed as the output.

Input / Output

C Program to Find Power of a Number

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...