+ 2
Need help in getting the sum of all the even numbers using while loop in java
My Homework
12 Answers
+ 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);
}
}
+ 5
Shivam Bajpai Please don't spam in question answer section. Read our guidelines:
https://www.sololearn.com/discuss/1316935/?ref=app
+ 4
But I'm struggling๐ฉ
+ 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++;
}
}
}
+ 3
Here
+ 3
I need to get the sum of the even numbers 2, 4, 6, 8, 10
+ 3
YAAAAS it works, thank you so much.๐
+ 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);
+ 3
Alright! Thank you for helping me.๐
+ 2
Okay, thank you ๐
+ 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++ :)
0
Ok im sorry