Explanation
The program calculates the sum of the sequence \( \frac{x^1}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \ldots \) where \( x \) is entered by the user and the number of terms \( n \) is also specified by the user.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Function Definitions
The program defines two functions: fact
and power
:
double fact(double x)
: Calculates the factorial of a given number \( x \).- Initializes
f
to 1. - Loops from 1 to \( x \), multiplying
f
by each value ofi
. - Returns the factorial
f
.
- Initializes
double power(double x, double y)
: Calculates \( x \) raised to the power of \( y \).- Initializes
p
to 1. - Loops from 1 to \( y \), multiplying
p
by \( x \). - Returns the result
p
.
- Initializes
Variable Declaration
The main()
function is defined, and several variables are declared:
double x, n, s = 0, p, f, i
: Variables to hold the input values, the sum, and intermediate values for power and factorial calculations.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the values of \( x \) and \( n \):
printf("Enter the value of x and n: ");
: Prompts the user to enter the values of \( x \) and \( n \).scanf("%lf %lf", &x, &n);
: Reads the values of \( x \) and \( n \) and stores them in the variables.
Sequence Sum Calculation
The program calculates the sum of the sequence:
for (i = 1; i <= n; i++)
: Loops through each term in the sequence.p = power(x, i);
: Calculates \( x^i \) using thepower
function.f = fact(i);
: Calculates \( i! \) using thefact
function.s += p / f;
: Adds \( \frac{x^i}{i!} \) to the sum.
Output Result
The program prints the sum of the sequence:
printf("Sum = %.lf \n", s);
: Prints the sum \( s \) formatted to one decimal place.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
double fact(double x)
{
double f = 1, i;
for (i = 1; i <= x; i++)
f *= i;
return f;
}
double power(double x, double y)
{
double p = 1, i;
for (i = 1; i <= y; i++)
p *= x;
return p;
}
int main()
{
printf("\n\n Program to generate total sum for the sequence x^1/1! + x^2/2! + x^3/3! + .....where x is entered by the user. \n\n");
double x, n, s = 0, p, f, i;
printf("Enter the value of x and n: ");
scanf("%lf %lf", &x, &n);
for (i = 1; i <= n; i++)
{
p = power(x, i);
f = fact(i);
s += p / f;
}
printf("Sum = %.lf \n", s);
return 0;
}
Output
Program to generate total sum for the sequence x^1/1! + x^2/2! + x^3/3! + .....where x is entered by the user.
Enter the value of x and n: 5 10
Sum = 145