Q:

Write C program to calculate power using while & for loop

0

Write C program to calculate power using while & for loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Calculating power of a number using while loop

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

    //Reading base & exponent
    printf("Enter base: ");
    scanf("%d",&base);
    printf("Enter exponent: ");
    scanf("%d",&exponent);

    power = 1;
    i = 1;
    //caculatinh power of given number
    while(i <= exponent)
    {
        power = power * base;
        i++;
    }
    printf("Power : %d", power);
    return 0;
}

Calculating power of a number using for loop

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

    //Reading base & exponent
    printf("Enter base: ");
    scanf("%d",&base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);
    power = 1;

    //caculatinh power of given number using for loop
    for(i=1; i<=exponent; i++)
        power = power * base;

    printf("Power : %d", power);
    return 0;
}

Result:

Enter base: 5

Enter exponent: 3

Power : 125

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to find factorial of any number... >>
<< Write C program to reverse a number using while & ...