C Program to print even and odd numbers between 1 to n and their sum


In this program, You will learn how to print even and odd numbers between 1 to n and their sum in C.


1 2 3 4 5 6 7 8 9 10

Even list is: 2 4 6 8 10

Odd list is: 1 3 5 7 9

Even sum is: 30, Odd sum is: 25


Example: How to print print even and odd numbers between 1 to n and their sum in C

Run it Now
#include<stdio.h>

int main() {

   int esum = 0, osum = 0, n, i;

   printf("Enter a Number Upto Even Odd Sum:");
   scanf("%d", &n);

   printf("\nEven List is :");

   for (i = 1; i <= n; i++) {
       if (i % 2 == 0) {
           esum = esum + i;
           printf("%d ", i);
       }
   }

   printf("\nOdd List is :");

   for (i = 1; i <= n; i++) {
       if (i % 2 != 0) {
           osum = osum + i;
           printf("%d ", i);
       }
   }

   printf("\nThe Sum of Even Numbers is :%d", esum);
   printf("\nThe Sum of Odd Numbers is :%d", osum);

   return 0;
}

Output:

Enter a Number Upto Even Odd Sum:10
Even List is :2 4 6 8 10
Odd List is :1 3 5 7 9
The Sum of Even Numbers is :30
The Sum of Odd Numbers is :25