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 even and odd numbers between 1 to n and their sum in c.

#include<stdio.h>

int main() {

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

  printf("Enter a number upto even odd sum:");
  scanf("%d", & n);

  printf("Even 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