Java program to check even numbers – In this article, we will discuss all the means to calculate even numbers in Java programming.
Suitable examples and sample programs have been added to the given article. The compiler has also been added so that you can execute the programs easily.
The means used in this piece are as follows:
Even numbers, as we all know, are any number which is completely divisible by two.
As you can see in the image uploaded above, these are the even numbers that fall between 1 – 100.
Since the definition of even numbers say that there shouldn’t be a remainder, thus, 0 is also an even number.
Hence, the multiple methods to calculate even numbers in Java programming are as follows:
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Even { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); long n=sc.nextLong(); Evennumbers(n); } static void Evennumbers(long n) { System.out.println("Even numbers are: "); for(long i=0;i<=n;i+=2) { System.out.print(i+" "); } } } |
1 2 3 4 |
output:1 Enter a number: 9 Even numbers are: 0 2 4 6 8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class Even { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); long n=sc.nextLong(); System.out.println("Even numbers are: "); for(long i=0;i<=n;i+=2) { System.out.println(i); } } } |
1 2 3 4 |
output:1 Enter a number: 20 Even numbers are: 0 2 4 6 8 10 12 14 16 18 20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Even { public static void main(String args[]) { long n=Long.parseLong(args[0]); System.out.println("Even numbers are: "); for(long i=2;i<=n;i+=2) { System.out.print(i+" "); } } } |
1 2 3 4 |
output:1 >java Even 5 Even numbers are: 0 2 4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Scanner; class Evennumbers { Evennumbers(long n) { System.out.println("Even numbers are: "); for(long i=0;i<=n;i+=2) { System.out.print(i+" "); } } } class Even { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); long n=sc.nextLong(); new Evennumbers(n); } } |
1 2 3 4 |
output:1 Enter a number: 19 Even numbers are: 0 2 4 6 8 10 12 14 16 18 |