+ 2

Need help in getting the sum of all the even numbers using while loop in java

My Homework

3rd Nov 2020, 6:05 AM
MS BLEA
12 Respostas
+ 5
/*Please check whether it's working or not.*/ public class Loops { public static void main(String [] args){ int x = 1,y=0; System.out.println("The sum of even numbers between 1 and 10 are the following:"); while(x<=5){ y+=x*2; x++; } System.out.println(y); } }
3rd Nov 2020, 6:31 AM
...
+ 5
Shivam Bajpai Please don't spam in question answer section. Read our guidelines: https://www.sololearn.com/discuss/1316935/?ref=app
5th Nov 2020, 7:25 AM
NightFox
NightFox - avatar
+ 4
But I'm struggling😩
3rd Nov 2020, 6:30 AM
MS BLEA
+ 3
public class Loops { public static void main(String [] args){ int x = 1; System.out.println("The even numbers between 1 and 10 are the following:"); while(x<=5){ System.out.println(x * 2); x++; } } }
3rd Nov 2020, 6:17 AM
MS BLEA
+ 3
Here
3rd Nov 2020, 6:17 AM
MS BLEA
+ 3
I need to get the sum of the even numbers 2, 4, 6, 8, 10
3rd Nov 2020, 6:27 AM
MS BLEA
+ 3
YAAAAS it works, thank you so much.😊
3rd Nov 2020, 6:35 AM
MS BLEA
+ 3
Use for loop in easy understanding.. int sum=0; for(int x=2; x<=10 ; x+=2) sum = sum+x ; System.out.println(sum);
3rd Nov 2020, 10:53 AM
Jayakrishna 🇮🇳
+ 3
Alright! Thank you for helping me.😊
3rd Nov 2020, 12:55 PM
MS BLEA
+ 2
Okay, thank you 😊
5th Nov 2020, 5:33 AM
MS BLEA
+ 1
Another way is for (int c = 1; c < n ;c+=1) if (c % 2 == 0) here will print c I don't know about java but I hope it'll be same as c++ :)
5th Nov 2020, 5:23 AM
Mani_K_A
0
Ok im sorry
5th Nov 2020, 7:25 AM
Shivam Bajpai
Shivam Bajpai - avatar