18 September 2018

C++ program to calculate x^n using recursion

//C++ program to calculate x^n using recursion
#include<iostream>
using namespace std;
int xn(int,int);
  int main()
    {
    int n,x,k;
    cout<<"Enter X value : ";
    cin>>x;
    cout<<"Enter N value : ";
    cin>>n;
    k=xn(x,n);
    cout<<"Power("<<x<<","<<n<<") = "<<k<<endl;
return 0;
}
    int xn(int x,int n)
   {
    int y=1;
    if(n>0 && n!=1)
       return (y*x)*xn(x,n-1);
    else if(n==0)
       return y;
    else
       return x*y;
   }
Output will be similar to this :



Similar program(s):
C++ program to calculate x power n(XN) 
   

No comments:

Post a Comment

To convert from Upper case to lowercase(A to a)

//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...