Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ หาผลรวมของอนุกรม (1/a + 2/a^2 + 3/a^3 + … + n/a^n)


ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาผลรวมของซีรีส์ที่ระบุ (1/a + 2/a^2 + 3/a^3 + … + n/a^n)

สำหรับสิ่งนี้ เราจะได้ค่า n และหน้าที่ของเราคือบวกทุกเทอมโดยเริ่มจากค่าแรกเพื่อหาผลรวมของอนุกรมที่กำหนด

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
//calculating the sum of the series
float calc_sum(int a, int n) {
   int i;
   float sum = 0;
   for (i = 1; i <= n; i++)
   sum += (i/ pow(a, i));
   return sum;
}
int main() {
   int a = 3, n = 4;
   float res = calc_sum(a,n);
   cout << res << endl;
   return 0;
}

ผลลัพธ์

0.716049