# C Program to Calculate the Sum of Odd & Even Numbers

In this program we will take a maximum value as input from the user. Starting from 1 we will run a for loop with increment by 1 and check if the number is even or odd. We will also create two variable as sum_of_even and sum_of_odd  numbers. Upon checking the even and odd value we will increase the value by one and at the end of the loop we will have the total number of even and odd values.

``````#include &lt;stdio.h&gt;

void main()

{

int i, num, sum_of_odd = 0, sum_of_even = 0;

printf("Enter the value of num\n");

scanf("%d", &amp;num);

for (i = 1; i &lt;= num; i++)

{

if (i % 2 == 0)

sum_of_even = sum_of_even + i;

}

else
{

sum_of_odd = sum_of_odd + i;

}

printf("Sum of all odd numbers = %d\n", sum_of_odd);

printf("Sum of all even numbers = %d\n", sum_of_even);

}``````
Comments to: C Program to Calculate the Sum of Odd & Even Numbers

### Trending

#### C Program to Find the Number of Integers Divisible by 5 and their sum  