C program to calculate the sum of odd and even numbers. Using the integer N, the software calculates the sum of odd and even numbers from 1 to N. Assume N is the number for which the sum must be found. Using the for loop, take each element one by one from 1 to N. To determine whether an element is even or odd, if,else expressions are utilised. Add the even and odd integers separately and store them in different variables.
int i, number, odd_sum = 0, even_sum = 0;
printf("Enter the value of number\n");
for (i = 1; i <= number; i++)
if (i % 2 == 0)
even_sum = even_sum + i;
odd_sum = odd_sum + i;
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
Enter the value of number
Sum of all odd numbers = 529
Sum of all even numbers = 506
How to calculate the odd or even number in c
The user must first specify the number up to which the total is to be calculated, which is saved in the variable num. Take the elements one by one from 1 to num using the for loop. Divide each element by 2 and use an if,else statement to determine whether it is odd or even. Set the odd sum and even sum variables to zero. If the element is even, add the element to the variable even sum. Separately print the variables odd sum and even sum, then leave.