Using while loop i want to print 10 even numbers

+4 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,080 points
93,225 views

4 answers to this question.

+3 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
• 58,020 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 5, 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)
–1 vote

C program to print EVEN numbers from 1 to N using while loop

  1. There are two variables declared in the program 1) number as a loop counter and 2) n to store the limit.
  2. Reading value of n by the user.
  3. Initialising loop counter (number) by 1 as initial value number =1.

Given a list of numbers, write a Python program to print all even numbers in given list. Using for loop : Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. # we can also print even no's using lambda exp.

answered Dec 16, 2020 by Gitika
• 65,770 points
–1 vote

To print the numbers from 1 to 10,

  1. We will declare a variable for loop counter (number).
  2. We will check the condition whether loop counter is less than or equal to 10, if condition is true numbers will be printed.
  3. If condition is false – loop will be terminated.
answered Dec 16, 2020 by Rajiv
• 8,870 points

Related Questions In Java

–1 vote
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 16, 2020 in Java by MIH
• 150 points
2,220 views
+1 vote
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
1,531 views
+1 vote
2 answers

Factorial of a number using while loop

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

answered Mar 19, 2020 in Java by leela
• 140 points
4,161 views
+1 vote
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,620 points
1,346 views
+1 vote
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,290 points
1,209 views
+1 vote
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
3,769 views
+1 vote
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
1,390 views
+17 votes
25 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, 2019 in Java by Suresh
• 720 points
253,805 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
• 58,020 points
1,260 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP