#include <iostream>
#include <cmath>
float tong(float x, int n) {
float s = 0;
for (int i = 1; i <= n; i++) {
s += pow(-1, i + 1) * x / i;
}
return s;
}
int main() {
float x;
int n;
std::cout << "Nhập x: ";
std::cin >> x;
std::cout << "Nhập n: ";
std::cin >> n;
float t = tong(x, n);
std::cout << "Tổng = " << t << std::endl;
return 0;
}
Text: Bài giai thừa
#include <iostream>
#include <cmath>
using namespace std;
float tong(int n){
float s=1,i=0,gt=1;
float tu=0;
for (i=1;i<=n;i++){
tu=pow (-2,i);
gt=gt*i;
s+mh= tu/gt;
}
return s;
}
int main(){
int n;
cout<<"Nhap n ";cin>>n;
cout<<"Tong: "<<tong(n);
}
.