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
832 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,140 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
44 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
77 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,310 points
55 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
44 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
37 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
27 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
232 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
27 views
+14 votes
17 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
57,400 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,140 points
89 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.