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 finds the Greatest Common Divisor (GCD) of two given numbers. Problem Statement Write a C program that...
This C program calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0....
This C program allows you to find the length of a linked list, which is the number of nodes present...