Thứ Ba, 28 tháng 6, 2016

Tinh S(n)=1 + 1/2! + 1/3! + ... + 1/n!

#include <iostream>
using namespace std;

int Nhap()
{
    int x;
    do
    {
        cin>>x;
        if(x<1)
            cout<<"Nhap sai, yeu cau nhap lai!";
    }while(x<1);
    return x;
}

float Tinh(int n)
{
    float s=0, p=1;
    for(int i=1; i<=n; i++)
    {
        p=p*i;
        s=s+(float)1/p;
    }
    return s;
}

void main()
{
    int n;

    cout<<"Nhap so nguyen n: ";
    n=Nhap();
    cout<<"Ket qua la: "<< Tinh(n);

    cout<<endl;
}

Không có nhận xét nào:

Đăng nhận xét