Publicidad
IN C++ Write a recursive function that takes two positive integers- a.docx
IN C++ Write a recursive function that takes two positive integers- a.docx
Próximo SlideShare
UNIT3.pptxUNIT3.pptx
Cargando en ... 3
1 de 2
Publicidad

Más contenido relacionado

Más de tristans3(20)

Publicidad

IN C++ Write a recursive function that takes two positive integers- a.docx

  1. IN C++ Write a recursive function that takes two positive integers, a and n, and returns the n th power of a, i.e. a n . Dont forget to consider the requirements for a recursive function. Solution solution.cpp #include <iostream>//header file for input output function #include <cmath>//header file for power function using namespace std;//it tells the compiler to link std namespace int power(int x,int n);//function declaration int main() {//main function int a, n; cout << "Enter base number a: "; cin >> a;//keyboard inputting cout << "Enter +ve power number n: "; cin >> n; cout << a << " ^ " << n << " = " << power(a, n)<<endl; return 0; } int power(int x,int n) {//function definition if ( n!=1 ) return (x*power(x,n-1)); //recursive function function calling itself } output Enter base number a: 2 Enter +ve power number n: 5 2 ^ 5 = 32
Publicidad