Learn about C Program to Find Power of a Number Using user-defined Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to Find Power of a Number Using user-defined Function
Program:
#include<stdio.h>
long power(int a, int b);
int main()
{
int num1, num2;
printf("Enter base and power: ");
scanf("%d %d",&num1, &num2);
long result = power(num1, num2);
printf("The result = %ld", result);
return 0;
}
// User-defined function to calculate power
long power(int a, int b)
{
long result = 1;
for(int i=1; i<=b; i++)
{
result *= a;
}
return result;
}
Output:
Enter base and power: 3 2
The result = 9
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to find the power of a number using pow() function
C program to find GCD of two numbers using functions