👤
a fost răspuns

Scrie un subprogram recursiv care calculeaza expresia E=1-2+3-4+ +(-1)n+1*n.

Răspuns :

► Expresia matematica recursiva a functiei :

[tex]E(n) = \left\{\begin{array}{ll} 0,\ daca\ n=0\\ E(n-1)+n, \ daca\ n - impar\\ E(n-1)-n, \ daca\ n - par\end{array} \right{, \forall n \in \mathbb{N}}[/tex]

► Program C++

#include <iostream>

using namespace std;

int fct(int n) {

if (n == 0) return 0;

else if (n % 2) return fct(n - 1) + n;

else return fct(n - 1) - n;

}

int main() {

unsigned n;

cout << "Introduceti n : ";

cin >> n;

cout << "E(" << n << ") = " << fct(n);

}