In this C program, we will learn how to write a program to calculate the sum of Odd and Even numbers.
Here is the code of the program to calculate the sum of Odd and Even numbers.
/*
C Program to Calculate the Sum of Odd & Even Numbers
*/
#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0){
even_sum = even_sum + i;
}
else{
odd_sum = odd_sum + i;
}
}
printf("Sum of all odd numbers between 1 to %d = %d\n",num, odd_sum);
printf("Sum of all even numbers between 1 to %d = %d\n",num, even_sum);
}
Enter the value of num
25
Sum of all odd numbers between 1 to 25 = 169
Sum of all even numbers between 1 to 25 = 156
--------------------------------
Process exited after 4.037 seconds with return value 46
Press any key to continue . . .
Comments