C-Programming

The following C program is to find the sum of the series 1+1/2+1/3+…+1/n. The following code have been compiled and executed in Dev C++.

Information about the code: 

> The variable is taken as double here. Int datatype will not be used here or else the generated output will be wrong.

> %.3lf prints 3 decimals numbers after the decimal, you can specify how many places you want to print.

Code:

#include<stdio.h>
void main(){
double n,i, sum=1;
printf("Enter any no.");
scanf("%lf",&n);
printf("1");
for(i=2;i<=n;i++){
sum=sum+(1/i);
printf(" + 1/%.3lf",i);
}
printf("\nTotal Sum is: %.3lf",sum);
}

Output:
Series in C

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.