C++ program to find sum of series 1+1/2+1/3
Here you will learn C++ program to find sum of series 1+1/2+1/3 upto n terms.
For Example
1+1/2+1/3…+1/n
Example of C++ program to find sum of series 1+1/2+1/3…+1/100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; void sumOfSeries(float k) { float i=1,sum=0,p; p=k; while(i<=k) { sum=sum+(1/i); i++; } cout<<"\n Sum of series upto "<<p<<" is "<<sum; } int main() { float terms; cout<<"\nEnter how many terms : "; cin >> terms; sumOfSeries(terms); return 0; } |
Output
Enter how many terms : 9
Sum of series upto 9 is 2.82897
Other Similar Programs
C++ program to find sum of series 1 + 2 + 3 +……+ n
C++ Program to print given series:1 2 4 8 16 32 64 128…. n
Check out our other C++ programming Examples