C Program To Find Sum of All Even Numbers Between Range, using For loop


Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user, using for loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 14 % 2 == 0. When we divide 14 by 2, it gives a reminder of 0. So number 14 is an even number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end If start value is greater than value present in end, we swap the values of variable start and end.

If user enters start = 5 and end = 14. C program finds all the even numbers between 5 and 14, including 5 and 14. So the even numbers are 6, 8, 10, 12, 14. We add all these even numbers and output the sum to the console window. i.e., 6 + 8 + 10 + 12 + 14 = 50. We out put the value 50 as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers

You can also watch C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers Between Range, using For loop


[youtube https://www.youtube.com/watch?v=9nKzyNnM2CY]

YouTube Link: https://www.youtube.com/watch?v=9nKzyNnM2CY [Watch the Video In Full Screen.]

Logic To Find Sum of All Even Numbers Between Range, using For loop

Step 1: We ask the user to enter start and end value.

Step 2: We initialize count to start and iterate through the for loop until value of count is less than or equal to value of variable end. For each iteration of for loop count value increments by 1.

Step 3: For every iteration we check if value present in variable count is a even number. i.e., count % 2 == 0. If this condition is true, then we add the value present in variable count to the previous value of variable sum.

Step 4: Once the control exits for loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Even Numbers Between Range, using For loop

#include<stdio.h>

int main()
{
    int start, end, temp, count, sum = 0;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);
    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }
    }

    printf("Sum of all the even numbers from %d to %d is %d\n", start, end, sum);

    return 0;
}

Output 1:
Enter start and end values
5
14
Even numbers between 5 and 14 are:
6
8
10
12
14
Sum of all the even numbers from 5 to 14 is 50

Output 2:
Enter start and end values
23
14
Even numbers between 14 and 23 are:
14
16
18
20
22
Sum of all the even numbers from 14 to 23 is 90

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *