C program to find sum of even numbers between 1 to n using loop

Previous Program Next Program

Write a C program to enter any number from user and find the sum of all even numbers between 1 to n. How to find sum of even numbers in a given range using loop in C programming. Logic to find sum of even numbers in a given range in C program.

Example

Input

Input upper limit of even number: 10

Output

Sum of even numbers between 1 to 10: 30

Required knowledge

Basic C programming, For loop

Logic to find sum of even number

Logic of finding sum of even numbers is almost similar to printing even number in range. In case you are in doubt about printing even numbers, here it is.

Below is the step by step descriptive logic to find sum of even numbers.

  1. Read upper limit to find sum of even number. Store it in some variable say N.
  2. Initialize another variable that will hold our sum with 0. Which is sum = 0.
  3. Initialize a loop from 2 to N and increment 2 on each iteration. The loop structure should look like for(i=2; i<=N; i+=2).
  4. Inside the loop body add the previous value of sum with i. Store the sum back in sum. Which is sum = sum + i.
  5. After the loop print the final value of sum.

Program to find sum of even numbers

/**
 * C program to print sum of all even numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n, sum=0;

    /* Read upper limit from user */
    printf("Enter upper limit: ");
    scanf("%d", &n);

    for(i=2; i<=n; i+=2)
    {
        /* Add current even number to sum */
        sum += i;
    }

    printf("Sum of all even number between 1 to %d = %d", n, sum);

    return 0;
}

Note: sum += i is similar to sum = sum + i. You can use any of them to find sum.

Output
Enter upper limit: 10
Sum of all even number between 1 to 10 = 30

Program to find sum of even numbers in given range

/**
 * C program to print sum of all even numbers in given range
 */

#include <stdio.h>

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

    /* Read lower and upper limit from user */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);


    // If start is not even then make it even
    if(start%2!=0)
    {
        start++;
    }

    for(i=start; i<=end; i+=2)
    {
        /* Add current even number to sum */
        sum += i;
    }

    printf("Sum of all even number between %d to %d = %d", start, end, sum);

    return 0;
}
Output
Enter lower limit: 6
Enter upper limit: 10
Sum of all even number between 6 to 10 = 24

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

10 comments:

  1. When I use this printf("%d\n",a); code in if condition it prints the even numbers correct but the sum comes 55
    #include
    main()
    {
    int n,a,sum=0;
    printf("Enter any number:");
    scanf("%d",&n);

    for(a=1;a<=n;a++)
    {
    if(a%2==0)
    printf("%d\n",a);
    sum=sum+a;
    }
    printf("Sum of all even numbers = %d\n",sum);
    system("pause");
    }

    And when i don't it prints 30. Can you please help me understand this ?

    #include
    main()
    {
    int n,a,sum=0;
    printf("Enter any number:");
    scanf("%d",&n);

    for(a=1;a<=n;a++)
    {
    if(a%2==0)
    sum=sum+a;
    }
    printf("Sum of all even numbers = %d\n",sum);
    system("pause");
    }

    ReplyDelete
    Replies
    1. @Gaurav what you are doing is a common beginner mistake.

      The reason why your both codes show's different result lies in the if statement.
      Now carefully look at your first for loop
      for(a=1;a<=n;a++)
      {
      if(a%2==0)
      printf("%d\n",a);
      sum=sum+a;
      }

      Here sum=sum+a is not a part of the if(a%2==2)
      it should be like

      for(a=1;a<=n;a++)
      {
      if(a%2==0)
      {
      printf("%d\n",a);
      sum=sum+a;
      }
      }

      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      now coming to your second for loop

      for(a=1;a<=n;a++)
      {
      if(a%2==0)
      sum=sum+a;
      }

      Here sum=sum+a is a part of for loop. Hence printing the correct output.

      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      Since you are using multiple statements under if() you must use { } read more at here Putting multiple statements under if else.

      Also using curly braces { } with if else or for loop is considered as a Good Programming Practice

      Delete
    2. Thanks man you are awesome I just started to learn coding so your posts help a lot.

      Delete
    3. One more thing if i don't use { after if condition then the sum lies in for condition and if i use that to keep it in the if condition. Is that it?

      Delete
    4. Yes you are right but its always considered as a good programming practice to use curly braces { }

      Delete
    5. You are always welcome @Gaurav Saymi. I am giving a little effort to share what i know.

      Keep visiting ;)

      Delete