Using while loop i want to print 10 even numbers

+3 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,040 points
29,073 views

4 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
• 58,100 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)
0 votes

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 15, 2020 by Gitika
• 65,870 points
0 votes

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 15, 2020 by Rajiv
• 8,880 points

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, 2020 in Java by MIH
• 160 points
988 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
263 views
0 votes
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
1,340 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, 2020 in Java by Niroj
• 82,680 points
178 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,640 points
295 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
274 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,464 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
197 views