From 0d96779069dd926658d6b58f4d1a97e4b2d10d25 Mon Sep 17 00:00:00 2001 From: Hugo Bezerra Date: Thu, 22 Oct 2020 03:03:45 -0300 Subject: [PATCH] Create recursive-power-of-num.c --- Programs/recursive-power-of-num.c | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Programs/recursive-power-of-num.c diff --git a/Programs/recursive-power-of-num.c b/Programs/recursive-power-of-num.c new file mode 100644 index 0000000..db0ae8b --- /dev/null +++ b/Programs/recursive-power-of-num.c @@ -0,0 +1,37 @@ +#include + +double pot( double a, int b ) +{ + if( b < 0 ) + { + a = 1 / a; + b = b * -1; + } + + if( b == 0 ) + return 1; + else if( b == 1 ) + return a; + + if(b > 1) + return a * pot( a, b-1 ); +} + +int main() +{ + int a, b; + + printf("Choose a number and a number to raise to the power of it respectively:\nExample: 2 3\n"); + + scanf( "%d %d", &a, &b ); + //a = (double) a; + + if( a == 0 && b == 0 ) + printf( "undefined\n" ); + else + { + printf( "%d raised to the power of %d equals %.3lf\n", a, b, pot( (double) a, b ) ); + } + + return 0; +}