Write a program in C++ to display the n terms of harmonic series and their sum. . 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
#include <iostream>
using namespace std;
void main()
{
int i,n;
float s=0.0;
cout<<"Input the number of terms : ";
cin>>n;
for(i=1;i<=n;i++)
{
if(i<=n)
{
cout<<"1/"<<i<<" + ",i;
s+=1/(float)i;
}
}
cout<<"\nSum of Series upto n terms :"<<s<<endl;
system ("pause");
}
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
.
Write a program in C++ to display the n terms of harmonic series and their sum. . 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Reviewed by Unknown
on
May 18, 2017
Rating:
No comments: