Write C program calculates the sum of odd and even numbers separately from 1 to n (value of n is entered by the user). The for loop starts with 1 and continue up to the number entered by the user. To check whether the number in the current iteration is odd or even, the modulus operator is used.
C program to calculate the sum of odd and even numbers - Source code
Program Output
Program Explanation
1. Two integer variables i and n are declared. Two integer variables sum_odd and sum_even are declared and initialized to 0.
2. For loop starts the first iteration with 1 and continue up to the number entered by the user (n).
3. To check whether the number in the current iteration is even or odd, modulus operator (%) is used. This operator divides the number by 2 and returns the remainder. If reminder returned is 0, the number is even otherwise it is odd.
4. The value of sum_even and sum_odd operators is updated inside the loop and printed when the loop ends.
need an explanation for this answer? contact us directly to get an explanation for this answer