This is a simple program to compute the sum of first 10 even and odd numbers and display the results.It is intended for beginner level learners of C programming.
The program is written and compiled using Dev-C++ 220.127.116.11 version compiler tool. You will find following sections in this post – problem definition, flowchart, program source code and verified output to help you understand and learn C programming language. Try to practice writing, compiling and run this program on your own.
The program for computing sum of first 10 even and odd numbers follow very simple logic, given below
- Take first 20 number, N = 20 of which half are even and half are odd numbers.
- Check each number one at a time and see if it’s divisible by 2.
- If Yes, add the number to Sum_Even.
- Else, add the number to Sum_Odd.
- Display the results
- End the program
Note:- Whenever a number is divisible by 2 it gives a remainder of 0, otherwise not.
Flowchart – Sum of Even and Odd
Program Code – Sum of Even and Odd
//Initialize the sum of even and sum of odd numbers
sum_even = 0;
sum_odd = 0;
// Total of 20 numbers comprise of 10 odd and 10 even numbers
// But you change the value of N to anything if required
N = 20;
//Compute the sum of even and sum of odd number
for(i = 1;i<=N;i++)
if((i % 2) == 0)
sum_even = sum_even + i;
sum_odd = sum_odd + i;
//Print the results now
cout <<; "Th sum of 10 evens are"
<< "\t" << sum_even << endl;
cout << "The sum of 10 odd are"
<<"\t" << sum_odd << endl;
The output of the program is given below. Remember that the program does ask for input because the value of N is already assigned. You can change that to a program asking input at run-time.