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
• 2,950 points
6,661 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,450 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
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
122 views
0 votes
1 answer

Factorial of a number using while loop

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

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

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 44,960 points
249 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 44,960 points
144 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
147 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,560 points
87 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,280 points
84 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,660 points
619 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
71 views