calculate the sum of even numbers and odd numbers seperately using c program




C program is to get the sum of odd and even numbers separately. So we need two separate sums as output, one for odd sum and one for even sum.

For this c program first we have to check the given number is odd or even, if the number is odd it should be added to odd sum and if it is even number it gets added to even sum.

We use a c program which uses a while loop for the numbers from 1 to n and we check the number is odd or even using the if loop then all even numbers are added in even sum and all odd is added in odd sum.

===================================================================
#include <stdio.h>
 
#include <conio.h>
 
void main()
 
{
 
   	int i, N, oddSum = 0, evenSum = 0;   // declares oddsum, evensum, i, n as integer. Defines oddsum and evensum as zero
 
	clrscr();
 
	printf("Enter the value of N\n");    // accepts the value of N
 
	scanf ("%d", &N);
 
	for (i=1; i <=N; i++)
 
  	{
 
		if (i % 2 == 0)
 
			evenSum = evenSum + i;  // for loop to check whether the number is odd or even and add to the respective sum
 
		else
 
			oddSum = oddSum + i;
 
	}
 
 
	printf ("Sum of all odd numbers  = %d\n", oddSum);     // display the odd and even sum
 
	printf ("Sum of all even numbers = %d\n", evenSum);
 
}
==================================================================
OUTPUT of C program to find the sum of odd, even numbers
==================================================================
RUN1

Enter the value of N
10
Sum of all odd numbers = 25
Sum of all even numbers = 30

RUN2
Enter the value of N
50
Sum of all odd numbers = 625
Sum of all even numbers = 650