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 <stdio.h>
void main()
{
int i, num, sum_of_odd = 0, sum_of_even = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= 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);
}
```

## No Comments

Leave a comment Cancel