C program to calculate the power of a number using Recurrsion
In this program we will read base and power and then
calculate result of that expression using recursion.
Below is a program to calculate the result of a given number, raised to the power of
n
using recursion.
Recurrsion:
Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process".
Source Code:
#include<stdio.h
// function prototype declaration
int power(int n1, int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d", &base);
printf("\n\nEnter Power factor: ");
scanf("%d", &exp);
printf("\n\n\n\t\t\t%d^%d = %d", base, exp, power(base, exp));
return 0;
}
int power(int b, int e)
{
if(e == 0)
return 1;
return (b*power(b, e-1));
}
No comments:
Post a Comment