C Program to Calculate the Sum of Odd and Even Numbers

C Programming Computer Science

In this C Program we are going to calculate the sum of even and odd numbers in between 1 to N.

  • Source Code

 #include <stdio.h>
 
void main()
{
    int i, num, oddsum = 0, evensum = 0;
 
    printf("Enter the value of num\n");
    scanf("%d", &num);
    for (i = 1; i <= num; i++)
    {
        if (i % 2 == 0)
            evensum = evensum + i;
        else
            oddsum = oddsum + i;
    }
    printf("Sum of all odd numbers  = %d\n", oddsum);
    printf("Sum of all even numbers = %d\n", evensum);
}

 

  • Runtime
Enter the value of num
100
Sum of all odd numbers  = 2500
Sum of all even numbers = 2550

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.