Monday, May 27, 2019

To Calculate the Power of a Number

Write a C Program to Calculate the Power of a Number using While Loop

Program:

#include <stdio.h>
int main()
{
    int base, exponent;

    long long result = 1;

    printf("Enter a base number: ");
    scanf("%d", &base);

    printf("Enter an exponent: ");
    scanf("%d", &exponent);

    while (exponent != 0)
    {
        result *= base;
        --exponent;
    }

    printf("Answer = %lld", result);

    return 0;
}

Output:

Enter a base number: 2
Enter an exponent: 4
Answer = 16
--------------------------------
Process exited after 11.73 seconds with return value 0
Press any key to continue . . .

0 Comments:

Post a Comment