Source Code:
- //C program to calculate sum of series.
- //1 + (1 / 2) + (1 / 3) + ...+ (1 / n)
- #include<stdio.h>
- #include<conio.h>
- int main(){ //Variable Declaration.
- float number, sum = 0, i;
- //Take input from User.
- printf("\n Enter the number: ");
- scanf("%f", &number);
- //Count upto n number using loop.
- for (i = 1; i <= number; i++)
- {
- sum = sum + (1 / i);
- if (i == 1)
- printf("\n 1 +");
- else if (i == number)
- printf(" (1 / %f)", i);
- else
- printf(" (1 / %f) + ", i);
- }
- printf("\n The sum of the given series is %f", sum); //print the output.
- }
Output:
Enter the number: 5
1 + (1 / 2.000000) + (1 / 3.000000) + (1 / 4.000000) + (1 / 5.000000)
The sum of the given series is 2.283334
0 comments:
Post a Comment