Using while loop i want to print 10 even numbers

+2 votes

I want to print 10 even numbers using while loop but I am unable to do that.

Here is my code,

public class WhileEven {

    public static void main(String[] args) {
        int i=0;
        System.out.println("Even numbers");
        while(i<=10)
        {
            int num=0;
            System.out.println(num);
            i+=1;
            num+=2;
        }
    }
}

can someone help as where i am going wrong?

Nov 22, 2018 in Java by Nitesh
• 3,020 points
15,487 views

2 answers to this question.

+1 vote

Hello @Nitesh, you are pretty much there. The only mistake you are doing is assigning the num inside the loop.

public class WhileEven {

    public static void main(String[] args) {
        int i=0;
        int num=0;
        System.out.println("Even numbers");
        while(i<=10)
        {
            System.out.println(num);
            i+=1;
            num+=2;
        }
    }
}

Now what was happening is that every time you were inside the loop the num gets assigned to 0. and you get the miscellaneous output. 

answered Nov 22, 2018 by Priyaj
• 57,640 points

how if its 

Write a program using While loop that adds all the even numbers less than 100 from  a given sequence of positive integeres. The sequence ends with -999.

+2 votes
public class Loops {
    public static void main(String [] args){
        int x = 1;
        System.out.println("Even numbers");
        while(x<=5){
            System.out.println(x * 2);
           x++;
        }
    }
}
answered Dec 4, 2019 by One of the Simplest ways to answer you q
Hey! Register to Edureka Community to earn points and reputations for every contribution(asking, answering or commenting)

Related Questions In Java

0 votes
0 answers

Write a program that uses a while loop to read 10 integer numbers from file

Write a program that uses a while ...READ MORE

Apr 15 in Java by MIH
• 160 points
281 views
0 votes
1 answer

How to find even or odd using call by value?

Call by value is, when a primitive ...READ MORE

answered Nov 23, 2018 in Java by Namitha
192 views
0 votes
2 answers

Factorial of a number using while loop

import java.util.*; public class Fact { ...READ MORE

answered Mar 19 in Java by leela
• 140 points
746 views
0 votes
1 answer

if i given invalid data how to get alert in selenium using java

Hello, Here is some of the method you ...READ MORE

answered Jul 20 in Java by Niroj
• 58,100 points
83 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,580 points
185 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
187 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
1,079 views
0 votes
1 answer

How to print factorial of a number using While loop?

While also works like for just you ...READ MORE

answered Nov 22, 2018 in Java by Namitha
117 views