QN019. Write a program to print sum of even number between 1 to 100.


1. Objective

To print the sum of even number between 1 to 100.

2. Theory

In this program, we use for loop to generate numbers between 1 to 100. We know that, if any number divided by 2 gives remainder 0 (zero) than this number is even number. So, we use if statement to get the even number and sum variable to store the result and print.

3. Source Code
public class Qn019{
public static void main(String[] args){
int sum = 0;
for(int i=1;i<=100;i++)
{
if(i%2==0)
sum+=i;
}
System.out.println("Sum of even number between 1 to 100 is " + sum);
}
}
4. Output

Sum of even number between 1 to 100 is 2550

5. Conclusion

In this way, I would like to conclude that, we can calcualte the sum of even numbers between 1 to 100 using this program.