๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R22

Programming for Problem Solving

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program to calculate the following, where x is a fractional value.i. 1-x/2 +x^2/4-x^3/6......

โ† All Lab Programs

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
flowchart for fractional sequence in c

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.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals