- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)
In this tutorial, we will be discussing a program to find the sum of the given series (1/a + 2/a^2 + 3/a^3 + … + n/a^n).
For this, we will be given with the value of n and our task is to add up every term starting from the first one to find the sum of the given series.
Example
#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; }
Output
0.716049
Advertisements