Write C program to calculate power using while & for loop

Write C program to calculate power using while & for loop


Introduction

Write C program to calculate power using while & for loop


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

Calculating power of a number using while loop


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#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

C language

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.