Write a program in C to display the n terms of harmonic series 1+1/2+1/3+1/4+1/5+⋯. and their sum


QUESTION:- Write a program in C to display the n terms of harmonic series 1+1/2+1/3+1/4+1/5+⋯. and their sum 

program


#include <stdio.h>

void main()

{

    int abi;

    float sum = 0;

    printf("enter the value of a: ");

    scanf("%d", &a);

    printf("enter the value of b for looping: ");

    scanf("%d", &b);

    for (i = 1i <= bi++)
    {

        sum = sum + (float)a / i;

        /* 

        5/1 = 5 

        5/2 = 2.5 

        5/3 = 1.667 

        5/4 = 1.25 

        sum = 1.25+1.667+2.5+25 

            = 10.47 

            = 5+2.5+1.67+1.25+0+0.83 

        */
    }

    printf("the sum of series is : %f"sum);
}


OUTPUT

enter the value of a: 5

enter the value of b for looping: 4

the sum of the series is: 10.416667


Follow our page for regular updates.

0 Comments