Skip to main content

C Program to find even ,odd numbers and their sum and average

Below program will accept the range for example 62 to 70. Program will count all even and odd numbers from range 62 to 70 and perform sum of even and odd numbers respectively. It also provide average of odd and even numbers.

// Program Name: Accept Range from user and then find even odd numbers with sum and average respectively.
#include<stdio.h>
 
void main()
{
 int startNum=0,endNum=0,i=0,eNoCnt=0,oNoCnt=0,eSum=0,oSum=0;
 
 printf("\n\t Provide Range of number to identify even number as requested for e.g 56 90");
 printf("\n\n\n\t Please provide Range\t");
 scanf("%d", &startNum);
 scanf("%d", &endNum);

 for(i = startNum; i < endNum; i ++)
 {
   if(i % 2 == 0)
   {
     eNoCnt++;
     eSum=eSum+i;
   }
   else
   {
     oNoCnt++;
     oSum=oSum+i;
   }
 }
  
 printf("\n\t Total Number of Even Numbers are = %d ", eNoCnt);
 printf("\n\t Total of Even number is %d",eSum);
 printf("\n\t Average of Even Number is %d",eSum/eNoCnt);
 printf("\n\n\t Total Number of Odd Numbers are = %d ", oNoCnt);
 printf("\n\t Total of Odd number is %d",oSum);
 printf("\n\t Average of Odd Number is %d",oSum/oNoCnt);
}

Output

Please provide Range   1
500

         Total Number of Even Numbers are = 249 
         Total of Even number is 62250
         Average of Even Number is 250

         Total Number of Odd Numbers are = 250 
         Total of Odd number is 62500
         Average of Odd Number is 250