Program to find the sum of series |C Program|
Introduction
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
Post a Comment
0 Comments