C Program to Find the Sum of Even and Odd Numbers

This is a C program to find the sum of odd and even numbers from 1 to N.

Problem Description

The program takes the number N and finds the sum of odd and even numbers from 1 to N.

Problem Solution

1. Take the number N upto which we have to find the sum as input.
2. Using for loop take the elements one by one from 1 to N.
3. Using if,else statements separate the element as even or odd.
4. Add the even and odd numbers separately and store it in different variables.
5. Print the sum separately and exit.

Program/Source Code

Here is source code of the C program to calculate the sum of odd & even numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  2. #include <stdio.h>
  4. void main()
  5. {
  6.     int i, num, odd_sum = 0, even_sum = 0;
  8.     printf("Enter the value of num\n");
  9.     scanf("%d", &num);
  10.     for (i = 1; i <= num; i++)
  11.     {
  12.         if (i % 2 == 0)
  13.             even_sum = even_sum + i;
  14.         else
  15.             odd_sum = odd_sum + i;
  16.     }
  17.     printf("Sum of all odd numbers  = %d\n", odd_sum);
  18.     printf("Sum of all even numbers = %d\n", even_sum);
  19. }
Program Explanation

1. User must first enter the number upto which he/she wants to find the sum and is stored in the variable num.
2. Using for loop take the elements one by one from 1 to num.
3. Use if,else statement for each element to find whether it is odd or even by dividing the element by 2.
4. Initialize the variables odd_sum and even_sum to zero.
5. If the element is even,then increment the variable even_sum with the current element.
6. If the element is odd,then increment the variable odd_sum with the current element.
7. Print the variables odd_sum and even_sum separately and exit.

Runtime Test Cases
Case 1:
Enter the value of num
Sum of all odd numbers  = 25
Sum of all even numbers = 30
Case 2:
Enter the value of num
Sum of all odd numbers  = 2500
Sum of all even numbers = 2550

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.