Problem 27

author
By Supreme2023-03-15
Problem Statement :
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n!
Code:

#include <stdio.h>

int main(){

//27. Write a C program to find 1/1! +1/2! +1/3! +1/4! +.....+1/n!.


int fact=1,n,i;
float sum = 0;

printf("\t Evaluate 1/1! +1/2! +1/3! +1/4! +.....+1/n!.");
printf("\n=====================================================================");

printf("\n\nEnter The Value Of N : ");
scanf("%d",&n);

for(i=1;i<=n;i++){
fact = fact * i;
sum = sum + (1.0/fact);
}

printf("\n_____________________________________________________________________");
printf("\n\nSum Of %d Terms Is : %.2f",n,sum);
printf("\n_____________________________________________________________________\n\n");
return 0;
}

Description :
  • The program is written in C and includes the standard input-output library stdio.h.
  • The program aims to evaluate the series 1/1! +1/2! +1/3! +1/4! +.....+1/n!.
  • Inside the main function, the required variables are declared: 'fact' to calculate the factorial, 'n' to store the number of terms and 'i' as a counter.
  • The printf function is used to display the series to be evaluated.
  • The user is prompted to enter the value of 'n' using the scanf function.
  • A for loop is used to calculate the factorials and sum the terms of the series. For each iteration, 'fact' is calculated as the factorial of the current value of 'i', and 1/fact is added to 'sum'.
  • The printf function is used again to display the result of the sum of the series.
  • The program ends by returning 0 to indicate successful execution.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.