Thứ Ba, 24 tháng 5, 2016

C/C++ | Bài 3 | Tính S(N) = 1^2 + 2^2 + ... + N^2


SOURCE CODE :

 S(N) = 1^2 + 2^2 + ... + N^2


#include <iostream>
using namespace std;

float calSum(int N)
{
 float S = 0;
 for (int i = 1; i <= N; i++)
 {
  S += (float)1 / i;
 }
 return S;
}

void main()
{
 int N;
 cout << "Input N = ";
 cin >> N;

 float result = calSum(N);
 cout << "Result = " << result;
}

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

Đăng nhận xét