programming-examples/c/Functions/C Program to Write a program using recursive function 'power' to compute x^n..c

38 lines
643 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
Write a program using recursive function 'power' to compute x^n
power(x,n) = 1 , if n = 0
power(x,n) = x , if n = 1
power(x,n) = x * power(x,n-1) , otherwise
*/
#include <stdio.h>
#include <conio.h>
float power(float x, int n)
{
if(n==0)
return 1 ;
else if(n==1)
return x ;
else
return ( x * power(x, n-1) ) ;
}
void main()
{
float x ;
int n ;
clrscr() ;
printf("Enter x and n: ") ;
scanf("%f %d", &x, &n) ;
printf("x raise to n = %f", power(x,n) ) ;
getch() ;
}
/*
Output:
Enter x and n: 2.5 3
x raise to n = 15.625000
*/