In: Computer Science
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
C++ code:
#include <iostream>
using namespace std;
//initializing product as 1
int product=1;
//initializing multiplyNumbers function
int multiplyNumbers(int n){
//checking if n is 1
if(n==1)
//returning product
return product;
else
//recursively calling multiplyNumbers fucntion and finding
product
return(n*multiplyNumbers(n-1));
}
int main(){
//calling multiplyNumbers function for a sample value and printing
result
cout<<multiplyNumbers(5)<<endl;
return 0;
}
Screenshot:
Output: