Write a C program that repeatedly receives a positive integer number from the user and display the results of the following equation: 1 1 1 1 ++ 2! 3! 4! (n+1)! Notel: If n <= 0 or n >= 8, display an error message and repeat the process by geting another input from the user. The program should terminate if the user does not want to try again. Sample execution: Please enter a positive integer value (between 1 and 7, inclusive): 6 1/2 + 1/6 + 1/24 + 1/120+ 1/720+ 1/5040 = 0.718 Do you want to try again? (1/yes, 0/no) 1 Please enter a positive integer value (between 1 and 7, inclusive): 1 1/2 = 0.500 Do you want to try again? (1/yes, 0/no) 1 Please enter a positive integer value (between 1 and 7, inclusive): 0 Invalid input. inclusive): 1 Please enter a positive integer value (between 1 and 7, 1/2 = 0.500 Do you want to try again? (1/yes, 0/no) 1 Please enter a positive integer value (between 1 and 7, inclusive): 7 1/2 + 1/6 + 1/24 + 1/120 + 1/720 + 1/5040 + 1/40320 = 0.718 Do you want to try again? (1/yes, 0/no) 0 Coodbuol +=+
/***************************************************************************** *
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
****************************************************************************** */
#include <stdio.h>
int main()
{
int e=1;
while(e)
{ int n;
puts("please enter a positive integer value (betweenn 1 and 7, inclusive)"); scanf("%d",&n);
if (n>7)
puts("Invalid input");
else if (n<=0)
puts("Invalid input");
else
{
long fact=1; float sum=0; int i=1;
while (i<=n) {fact=fact*(i+1);
sum= sum +(1.0/fact); if (i<n) printf("1/%ld+",fact); else printf("1/%ld=",fact); i++;
}
printf("%.3f\n",sum); }
puts("Do you want to try again? 1/yes, 0/no"); int answer;
scanf("%d",&answer);
if (answer==0)
e=0;
} puts("Goodbye!");
return 0; }
Step by step
Solved in 2 steps with 3 images