Home

A program to find the sum of even numbers and the sum of odd numbers below 1000

C Programming Language 038

The following code shows how to write a program to find the sum of even numbers and the sum of odd numbers below 1000.

Program

#include <stdio.h>

void main()
{
    int i, n = 1000;
    long int esum = 0, osum = 0;

    for (i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            esum = esum + i;
        }
        else
        {
            osum = osum + i;
        }
    }
    printf("Sum of odd numbers = %ld\n", osum);
    printf("Sum of even numbers = %ld\n", esum);

    printf("\n");
}

Result

Sum of odd numbers = 250000
Sum of even numbers = 249500


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments