# Exponential Program-x power n-using Recursion in C

Another Recursion Program? Isn’t this a headache? Not anymore. Just copy paste the program below and enjoy the output.

Simple Program for +ve powers of x, i.e n=0,1,2,3…Complex Program after this Recommended Read : Size of an array using Recursion in C

``#include<stdio.h>  int main() { int num, pow; printf("Enter The Number and the power of the Number"); scanf("\%d \%d", &num, &pow);  printf("Result is \%d",result(num, pow)); //here a function call is directly printing the value returned by the function result(). return 0; }  int result(int n, int p) { if(p==0) return 1;//any number raised to 0 is 1 if(p==1) return n; else return n*result(n,p-1); } ``

Output Variables: (are in single quotes ‘ ’ ) ‘num’ holds the number entered entered by the user.
‘pow’ holds the power to which the number is raised.

``printf("Enter The Number and the power of the Number"); scanf("\%d \%d", &num, &pow); ``

scanf() takes the input from the screen and stores it in variable by the help of format specifier (\%) which specifies whether the input will be an integer(\%d) or float(\%f) or characters(\%c). (&) Specifies in which variable it must be stores.

printf(«Result is \%d»,result(num, pow)); //here a function call is directly printing the value returned by the function result().

In the above snippet,inside the printf() function, function call is made to result() sending the number and its power to it. This function returns the answer of the exponential sent to function, which we will see ahead.

``int result(int n, int p) { if(p==0) return 1;//any number raised to 0 is 1 if(p==1) return n; else return n*result(n,p-1); }`` Le’t look at negative exponential power

``#include<stdio.h>  int main() { int num, pow; float nnum; printf("Enter The Number and the power of the Number"); scanf("\%d \%d", &num, &pow); if(pow>0) printf("Result is \%d",result(num, pow)); else { nnum = result(num, -pow); //float to hold fractions printf("Result is \%f", 1/nnum); }  return 0; }  int result(int n, int p) { if(p==0) return 1;//any number raised to 0 is 1 if(p==1) return n; else return n*result(n,p-1); }``

Output There is no change in function result(), only a few lines of code in main() are manipulated.

We declare an extra variable ‘nnum’ representing negative number (just to understand we are calling it that)

Extra if else statement is used to check if the power is negative, if power is positive, it executes the same as above,

Else,
We know that when power is negative of a number, The number is in the form 1/x . So what we do here is that calculate the result as above, and then find out the reciprocal and display the fraction, as ‘1/nnum’. As simple as that.

Also, don’t forget to subscribe to our Newsletter.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: