Using while loop i want to print 10 even numbers

0 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,870 points
1,301 views

1 answer to this question.

0 votes

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
• 56,160 points

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
50 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
84 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
83 views
0 votes
1 answer

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

To print current date and time using ...READ MORE

answered Apr 13, 2018 in Java by Akrati
• 3,150 points
55 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,520 points
42 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
32 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,320 points
279 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
28 views
+14 votes
18 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7 in Java by Suresh
• 700 points
63,809 views
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 56,160 points
95 views