Assignments » Flow Of Control » Set2 » Solution 13

Write a program to calculate the sum of following series where n is input by user. 
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

 

#include<iostream>
using namespace std;

int main()
{
	int i,n;
	float sum=0;

	cout<<"Enter the value of n ";
	cin>>n;

	for(i=1;i<=n;i++)
		sum += 1.0/i;

	cout<<"Sum : "<<sum;

	
	return 0;
}

Enter the value of n 5
Sum : 2.28333

 

«back