# C PROGRAMS
Function
➤ Introduction to Function
➤ User-defined Functions
➤ Recursion in C
➤ Storage Classes in C
➤ Scope of Variables
Function Programs in C
➤ Functions Examples in C
➤ Add 2 Numbers Function
➤ Find Sum of N numbers
➤ Largest of Three Numbers
➤ Add Subtract Multiply Divide
➤ Calculator Program Functions
➤ Factorial Program Function
➤ GCD of Two Numbers
➤ Find Power of a Number
➤ Find Reverse a Number
➤ Fibonacci Series Function
➤ Prime Number Function
➤ Palindrome Number in C
➤ Armstrong Number in C
Recursion Programs in C
➤ Recursion Examples in C
➤ Fibonacci Series
➤ Find Factorial
➤ Find GCD or HCF
➤ LCM Using Recursion
➤ Tricky C Programs
We will write a C program to find the power of a number using a function. First, we will use the pre-defined function pow(), and later we will develop a user-defined function.
C program to find the power of a number using pow() function
In C language, the pow() is a pre-defined function that is used to calculate the power of the number. It takes two arguments. It is defined in the header file “math.h
”. SO, to use the pow() function, we must include this header file in our program.
Function prototype:- pow(x,y);
The function pow() calculates the power of y. If the x is negative, y must be an integer. If the x is zero, y must be a positive number.
#include<stdio.h>
#include<math.h>
int main()
{
int num1, num2;
printf("Enter base and power: ");
scanf("%d %d",&num1, &num2);
printf("Result = %.2f",pow(num1, num2));
return 0;
}
Output for the different test-cases:-
Enter base and power: 2 5
Result = 32.00
Enter base and power: 5 4
Result = 625.00
Using a user-defined function
We can also write a C program to find the power of a number without using pow(). In the below program power() is a user-defined function that calculates the power of a number. Previously we have already written a program to find the power of a number using loops. Based on that program we can write the same using function.
Prerequisites for this program:- Introduction to Function in C, User-defined Functions in C, C Program Using Functions Example
#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 for the different test-cases:-
Enter base and power: 3 3
The result = 27
Enter base and power: 7 2
The result = 49
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!