Write a C program to find 1+1/2+1/3+1/4+....+1/n.

 

 GTU Programming for Problem Solving Practical-26


// 26. Write a C program to find 1+1/2+1/3+1/4+....+1/n.

#include <stdio.h>
#include <math.h>

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

    printf("\nEnter the number till which you wanted series:");
    scanf("%f", &n);

    printf("\n0");

    for (float i = 1; i <= n; i++)
    {
        printf("+ 1/%.f", i);
        sum = sum + (1/i);
    }
    printf("\n = %f", sum);
    return 0;
}

Output:

Enter the number till which you wanted series:10

0+ 1/1+ 1/2+ 1/3+ 1/4+ 1/5+ 1/6+ 1/7+ 1/8+ 1/9+ 1/10
 = 2.928968

Post a Comment

0 Comments