Program to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........

Problem:

WAP to display the series 0.9, 0.99, 0.999, 0.9999, .......... and also find the sum of the series.

Source Code:

/*to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int i, n;
    double j=0, sum=0;
    printf("Enter the length of the series: ");
    scanf("%d", &n);
    printf("The series is\n");
    for(i = 1; i <= n; i++)
    {
        j += 0.9/pow(10, i-1);
        printf("%f, ", j);
        sum += j;
    }
    printf("\nSum of  the series is %f", sum);
    system ("pause");
    return 0;
}

Sample Run:

Enter the length of the series: 5
The series is
0.900000, 0.990000, 0.999000, 0.999900, 0.999990,
Sum of  the series is 4.888890

No comments:

Post a Comment