Alternating series: 1 - 2 + 3 - 4 + ... Dry Run in C

Alternating series: 1 - 2 + 3 - 4 + ... is an interactive C dry run visualizer from the While programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Alternating series: 1 - 2 + 3 - 4 + ... Program Code

#include <stdio.h>
int main() {
    int n, i = 1, sum = 0;
    
    printf("Enter n: ");
    scanf("%d", &n);
    
    while(i <= n) {
        if(i % 2 == 0)
            sum = sum - i;  // Even: subtract
        else
            sum = sum + i;  // Odd: add
        i++;
    }
    
    printf("Sum = %d\n", sum);
    return 0;
}

View the complete While programs page.

Program Console Alternating series: 1 - 2 + 3 - 4 + ... Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.