info@meritcampus.com    +91-85006-22255
...
`

Write a program to find the sum of all even numbers till a given number.

Input Output
`7` `2 + 4 + 6 = 12`
`15` `2 + 4 + 6 + 8 + 10 + 12 + 14 = 56`

``` class SumOfEvenNumbers {     public static void main(String s[])    {        System.out.println("Sum of even numbers till 5 is " + sum_of_even_numbers(5));    }     public static int sum_of_even_numbers(int input)     {         int output = 0; ```
```         return output;     } } ```
Topic:

### User comments below.All of them might not be correct.

take a variable for keeping sum. then take a loop(for) and incriment if by 2 till loop index value less then input value. print the loop indices with a '+' symbol and after completing the look pring symbol '=' and sum variable

Posted by Ashok Kumar Paritala    2014-11-28 05:36:53

here we have to use the loops....solve this using the loop... Initializing loop control let us take i variable to 1 and put condition to loop control i<=input...inside the for loop for every iteration check the 'i' is even or Not...this can Check by when we divide the i by 2 the remainder must 0 than we can say that i is Even..to get the Remainder we use the %..i%2==0 than we can add i to output...

Posted by Uday Kumar    2014-11-28 05:41:38

For i=1 to input
Chek if i%2==0
Then output = output+i
End if
End for
Return output

Posted by Asma Mujtaba Khan    2014-11-28 05:48:56

in this program we don't even need if conditions,ly use looping concept.                    example : 1)    in for loop initialize one local variable with 0,                                         2)   give one condition local variable must be less than or equal to given input number.                                                                                                                          3)    thn use increment operator,(increment by 2 each time)                                        4)     thn just add local variable value with output for each iteration.                                     5)     condition used in the for loop fails thn control come out of loop thn returns output value.

Posted by Maheshwari Natarajan    2014-11-28 18:56:29

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-29 06:10:40

hey dats unfair

Posted by Asma Mujtaba Khan    2014-11-29 17:10:53

the qstn simply ask to write th code n not th stmt wise explntn... these stmt wont make a prgrm to compile...

Posted by Asma Mujtaba Khan    2014-11-29 17:11:56

Asma Mujtaba Khan, this contest is for providing the hints on how to solve the problem and not to provide the code. Please see the rules at  http://java.meritcampus.com/program-hints . Also we suggest to use complete word like question, statement, explanation instead of qstn, stmt, explntn. Because it makes it very difficult to understand this. Thanks for participating and better luck next time.

Posted by Merit Campus    2014-11-30 15:31:34