Write a C program to calculate the following, where x is a fractional value.i. 1-x/2 +x^2/4-x^3/6......
Write a C program to calculate the following, where x is a fractional value.i. 1-x/2 +x^2/4-x^3/6......
Algorithm
start
step1: result := 1
step2: Read x,n
step3: for i := 1 to n step 1 do
step4: if i % 2 = 0
step5: term := 1
step6: else term := -1
step7: term := term * (xi / 2*i)
step8: result := result + term
step9: Print result
stop
Flowchart
C Programming
//1-x/2 +x^2/4-x^3/6......
#include <stdio.h>
#include <math.h>
int main() {
double x, result = 1.0,term; // Initialize result to 1 for the first term
int n,i;
// Input: Get the fractional value of x and number of terms from the user
printf("Enter a fractional value for x: ");
scanf("%lf", &x);
printf("Enter the number of terms (n): ");
scanf("%d", &n);
// Calculate the expression up to n terms
for (i = 1; i <= n; i++) {
// Calculate each term
term = (i % 2 == 0 ? 1 : -1) * (pow(x, i) / (i*2));
result += term; // Add the term to the result
}
// Output: Display the result
printf("The result of the expression is: %lf\n", result);
return 0;
}
OUTPUT
Enter a fractional value for x: 2
Enter the number of terms (n): 3
The result of the expression is: -0.333333
๐ป Online C Compiler
Run and test this C program online. You can also edit the code before executing it.
๐ก Edit the program in the compiler and click
Copy Program
to copy the latest edited code.
Related Lab Programs
๐ป
Practice Sessions
1.
Write a simple program that prints the results of all the operators available in C.
View Solution โ
2.
Write a simple program that converts one given data type to another using auto conversion and casting.
View Solution โ
๐ข
Simple Numeric Problems
3.
Write a program to display the class awarded based on the percentage obtained by a student.
View Solution โ
5.
Write a program that shows the binary equivalent of a given positive number between 0 to 255.
View Solution โ
โ
Expression Evaluation
4.
Write a C program to find the sum of individual digits and test whether the number is palindrome.
View Solution โ
๐งฎ
Arrays, Pointers and Functions
1.
Write a C program to find the minimum, maximum and average in an array of integers.
View Solution โ
2.
Write a C program to find the mean, variance and standard deviation of an array and sort the array.
View Solution โ
4.
Write a C program that uses functions to perform Multiplication of two matrices.
View Solution โ
5.
Write a C program to find the transpose of a matrix using dynamically allocated memory.
6.
Write a C program to find factorial using recursive and non-recursive functions.
View Solution โ
7.
Write a C program to find the greatest common divisor using recursive and non-recursive functions.
View Solution โ
8.
Write a C program to calculate x raised to the power of n using recursive and non-recursive functions.
View Solution โ
๐
Files
2.
Write a C program which copies one file to another, replacing lowercase characters with uppercase equivalents.
3.
Write a C program to count occurrences of a character in a text file using command line arguments.
4.
Write a C program to create and update a binary file using fseek.
๐ค
Strings
1.
Write a C program to convert a Roman numeral in the range I to L into its decimal equivalent.
2.
Write a C program to convert a number between 1 and 50 into its Roman numeral equivalent.
3.
Write a C program that uses functions to insert a sub-string into a given main string.
View Solution โ
4.
Write a C program that uses functions to delete n characters from a given position.
View Solution โ
๐
Miscellaneous
1.
Write a menu-driven program to find the smallest, largest, sum and average.
2.
Write a C program to construct pyramids using stars or numbers.
๐