C Program: Alternating Sign Power / Factorial Series

Written: August 22, 2026

Series labs combine three ideas: a running sum, a sign that flips, and a term built from power and factorial.

This walkthrough keeps the logic small and readable so you can type it, run it, and then change one input at a time to see what happens.

What you will build

You will compute terms like x^n / n! with alternating signs and add them into sum.

  • Read x and number of terms n
  • For i = 1..n build term = (±) pow(x,i) / fact(i)
  • Add term to sum
  • Print sum

Working C example

#include <stdio.h>
#include <math.h>

long long fact(int n) {
    long long f = 1;
    int i;
    for (i = 2; i <= n; i++) f *= i;
    return f;
}

int main(void) {
    int n, i, sign = 1;
    double x, sum = 0.0, term;

    printf("Enter x and terms: ");
    scanf("%lf %d", &x, &n);

    for (i = 1; i <= n; i++) {
        term = sign * pow(x, i) / (double)fact(i);
        sum += term;
        sign = -sign;
    }

    printf("Sum = %.6f\n", sum);
    return 0;
}

Advertisement

How the logic works

Read the program top to bottom: includes and main first, then the statements that change variables, then the print that proves the result.

If your output looks wrong, print the variables before and after the critical lines. That single habit catches most beginner bugs faster than rewriting the whole file.

Compile with -lm if your toolchain needs it for pow. Print each term once while debugging the sign flip.

Advertisement

Common mistakes

Forgetting a semicolon, using the wrong format specifier in printf/scanf, and mixing up assignment (=) with comparison (==) are the usual culprits.

Compile with warnings enabled (`gcc -Wall`) so the compiler points at risky casts and unused variables before you chase them by hand.

Try this next

Change the sample inputs, add a second test case, and briefly note what stayed the same. Teaching yourself with tiny experiments sticks better than copying a longer program you never run.

When you can explain every line without looking, you are ready for the next exercise in the series.

Keep learning

If this walkthrough on alternating series factorial c helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

C Program: Count People by Height and Weight Conditions

Next Article

Python Program to Calculate the Area of a Triangle

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨