C Language "Sum of series 1+1/2+1/3+......+1/n using for loop" program with output

 #include <stdio.h>

int main() { int n; double sum = 0.0; printf("Enter the value of n: "); scanf("%d", &n); if (n <= 0) { printf("Invalid input. Please enter a positive integer.\n"); } else { for (int i = 1; i <= n; i++) { sum += 1.0 / i; } printf("Sum of the series 1 + 1/2 + 1/3 + ... + 1/%d is: %lf\n", n, sum); } return 0; }



Output

Enter the value of n: 5 Sum of the series 1 + 1/2 + 1/3 + ... + 1/5 is: 2.283333

Comments

Popular posts from this blog

Video Formats

Where video editing is used

File saving and folder management