recursion

Write a recursive function that prints the following series:
1/1!+1/2!+1/3!+1/4!+⋯+1/𝑛!
Write only the function. You cannot use loops. Use the empty box below to write your answer. Calculating the sum of the series is not necessary, just print the series.
Following is an example of how the function can be called and its output.
dont know how to do this code. plz help me out here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>

using namespace std;

float invRecStatement(int);
void printFactorial(int);
float sum(int);

int main()
{
    //snazzy intro
    cout << "Inverse Factorial Finder!\n=========================\n\n";

    //enter a value x to start factorial
    int x;
    cout << "enter a number to begin the factorial: ";
    cin >> x;


    printFactorial(x);


    //might as well get the sum right?
    cout << "the sum of the inverse factorial is: " << sum(x) << endl;
}


float invRecStatement(int fac)
{
    //break statement
    //
    //you need to include this in your recursive statement to avoid
    //infinite loop.
    if(fac == 1)
    {
        return 1;
    }
    int factorial = fac * invRecStatement(fac - 1);


    //returns the passed value multiplied by the function of the passed value minus one
    return factorial;
}

void printFactorial(int num)
{
    //this prints the algorithm however many times the number entered in the program
    //
    //the incremental operator and conditional were shifted to one to avoid dividing by zero at start.

    for(int inc = 1; inc < num + 1; inc++)
    {
        cout << "1/" << invRecStatement(inc) << " + ";
    }
    cout << endl;
}

float sum(int num)
{
    float accumulatedValue = 0;
    for(int inc = 1; inc < num + 1; inc++)
    {
       accumulatedValue += 1/invRecStatement(inc);
    }
    return accumulatedValue;


i didn't catch the no using loops bit but i assume that he or she is only talking about the actual math bit, if not you could always replace the loops with a recursive void that counts down an int. what I did was break up the algorithm into two parts, the numerator one and the factorial part, since the "1/" part always occurs and you just need the factorial you can put it in a recursive function or loop that prints out that part and then feed a value into a parameter calculating the actual factorial.

on the first loop i shifted everything up one because the program divided the first part by zero, i feel that doing that was just an easy out, would anyone know of a better way?
This answers the question as stated, now figure out how to not print the last "+" :-)

1
2
3
4
5
6
7
8
9
10
void print_fact(int n)
{
	int prn = n;
	if (n > 1)
	{
		print_fact(--n);
	}
	
	printf("1/%d! + ", prn);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void print_terms_from( int n, int max_n )
{
    std::cout << " + 1/" << n << '!' ; // print termn
    if( n < max_n ) print_terms_from( n+1, max_n ) ; // print termn+1 onwards
}

void print_series_till( int max_n )
{
    std::cout << "1/1!" ; // print the first term
    if( max_n > 1 ) print_terms_from( 2, max_n ) ; // print remaining terms
    std::cout << '\n' ; // and finally, print a new line
}

int main() { print_series_till(18) ; }

http://rextester.com/QFOC6792
Topic archived. No new replies allowed.