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. C program to find sum of all even numbers in a given range.

Input upper limit to find sum: 10

Output sum of even numbers between 1 to 10: 30

As I talked many times about even and odd numbers in my previous post, also I explained many methodology to check even or odd numbers such as by using - bitwise operators, conditional operators, if else, switch case etc. Also I have explained how to display even numbers in a given range. We will use the same logic that we have used in generating even numbers between a range to find the sum of all even numbers in given range.

Happy coding ;)

**Example:**Input upper limit to find sum: 10

Output sum of even numbers between 1 to 10: 30

### Required knowledge

Basic C programming, For loopAs I talked many times about even and odd numbers in my previous post, also I explained many methodology to check even or odd numbers such as by using - bitwise operators, conditional operators, if else, switch case etc. Also I have explained how to display even numbers in a given range. We will use the same logic that we have used in generating even numbers between a range to find the sum of all even numbers in given range.

### 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; /* Reads range from user */ printf("Enter any number: "); scanf("%d", &n); for(i=2; i<=n; i+=2) { /* Adds each subsequent even numbers to sum */ sum += i; } printf("\nSum 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. After learning this program you might want to increase your knowledge quotient by learning the same using recursive approach. Learn to find sum of even numbers using recursion.

Output

Enter any number: 10

Sum of all even number between 1 to 10 = 30

Sum of all even number between 1 to 10 = 30

Happy coding ;)

### You may also like

- Loop programming exercises index.
- C program to check even or odd number using functions.
- C program to print all odd numbers between 1 to 100.
- C program to find sum of all odd numbers between 1 to n.
- C program to find sum of all natural numbers between 1 to n.
- C program to find sum of all prime numbers between 1 to n.
- C program to find all factors of a number.
- C program to print number in words.
- C program to find sum of first and last digits of a number.
- C program to check Prime number.
- C program to check Armstrong number.

(y)

ReplyDeleteThanks @ishikaa keep visiting :)

DeleteWhen I use this printf("%d\n",a); code in if condition it prints the even numbers correct but the sum comes 55

ReplyDelete#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");

}

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

DeleteThe 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 PracticeThanks man you are awesome I just started to learn coding so your posts help a lot.

DeleteOne 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?

DeleteYes you are right but its always considered as a

Deletegood programming practiceto use curly braces { }You are always welcome @Gaurav Saymi. I am giving a little effort to share what i know.

DeleteKeep visiting ;)

I want know about coding

ReplyDeleteWhat you want to know @phayero

Delete