Here is a recursive function that takes a number inputted by the user and find the sum of all numbers from 1 to that number:

#include <iostream>

using namespace std;

int recursion(int num);

int main(){

int num = 0;

int sumNum;

cout << “Enter any integer greater than 0: ” << endl;

cin >> num;

sumNum = recursion(num);

cout << “The sum is: ” << sumNum << endl;

cin.ignore();

cin.get();

return 0;

}

int recursion(int num)

{

int sum = 0;

if(num == 1){

return 1;

}

else{

sum = recursion(num-1) + num;

return sum;

}

}

Happy coding!

Advertisements