Program to find the sum of series |C Program|

Introduction

     In the realm of mathematics, series play a significant role, and their summation often leads to intriguing results. In this blog post, we will unravel a C program designed to calculate the sum of a particular series. We'll delve into the code, examine its structure, and gain insights into how mathematical concepts are implemented in programming.  


Algorithm

Include Header Files:

Include necessary header files like conio.h, math.h, and stdio.h.

Define Series Function:

Define a function series that calculates the sum of a series up to a given term.

Initialize Sum:

Initialize a variable sum to 0.

Loop for Series Calculation:

Use a loop to iterate over each term of the series.

Calculate and Update Sum:

Calculate each term using the formula 1 / pow(i, i) and add it to the running sum.

User Input in Main:

Ask the user to input a value for n.

Function Call in Main:

Call the series function with the user-input value and store the result in a variable.

Print Result:

Print the calculated sum of the series.

      
     
         // Program to find the sum of series
   #include<stdio.h>
   #include<conio.h>
   #include<math.h>
   double series(int n) {
    int i;
    double sum = 0, s;
    for (i = 1; i <= n; i++) {
      s = 1 / pow(i, i);
      sum = sum + s;
    }
    return sum;
  }
  void main() {
    int n;
    double result;
    clrscr();
    printf("enter the value for n= ");
    scanf("%d", &n);
    result = series(n);
    printf("sum of series=%.4f", result);
    getch();
  }
       
     


Output

    
         // Output
         enter the value for n= 5
         sum of series=1.2913  
       
     


Conclusion

     In this blog post, we explored a C program designed to compute the sum of a series. The program showcases the application of mathematical concepts, loops, and function usage in programming. Understanding how mathematical formulations are translated into code not only provides insights into programming logic but also highlights the versatility of C in handling complex mathematical operations. This example serves as a valuable illustration for those interested in the intersection of mathematics and programming.

Post a Comment

0 Comments