Tech Study

Power in C Programming | C Pow() Function

Power in c uses pow() function to calculate power of a number, which must use “#include<math.h>”, given by the user.

What is Power function in c

The pow() function determines raised to the power of y,given two numbers and their base and exponent which is “xy” and returns x raised to the power of y. Essentially, the c pow() function is used to determine exponent value and calculate the number’s power, which must use #include<math.h>. 

Example – pow(5 , 3); Then we will get the result as 5^3, which is 125.

Syntax

syntax for C Pow() Function:

double pow(double x, double y);

c pow() Parameters: 

Function takes two parameters:

  • x – The base value
  • y – The power value

Example of power in c programming:

From this examples learn how to raise to a power in c

Example 1:

Calculating power of a number in c 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

Write C program to calculate power using while & for loop
Write C program to calculate power using while & for loop

TaggedWrite C program to calculate power using while & for loop

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more