Introduction
This is a tutorial for finding even numbers from array. The program is given below that takes numbers from user and saves them in array and find even numbers and prints them. The program is extendable. Go enjoy the program. Lets begin…………
Program for finding even numbers from array.
//import Scanner as we require it.
import java.util.Scanner;
// the name of our class its public
public class ArrayEvenNumber {
//void main
public static void main (String[] args)
{
//declare int
int i,x,a[] = new int[10];
//Declare input as scanner
Scanner input = new Scanner(System.in);
//Take input for arrays
for(i=0;i<5;i++)
{
System.out.println("Enter Number :");
a[i] = input.nextInt();
}
System.out.println("Even Numbers are :-");
//print the even numbers
for(i=0;i<5;i++)
{
x=a[i]%2;
if(x==0)
System.out.println(a[i]);
}
}
}
Output
Enter Number :
1
Enter Number :
2
Enter Number :
3
Enter Number :
4
Enter Number :
5
Even Numbers are :-
2
4
How does it work
- You enter the number.
- The number is saved in respective array.
- The numbers from arrays are taken and tested if even or odd.
- The even number is printed.
Extending it
The program can be extended by doing the same program for finding odd number. Go extend it.
Explanation.
- Import the Scanner.
- Declare the class as public
- Add the void main function
- Declare input as Scanner.
- Add a for loop.
- Add system.out.println() function with the message to enter number.
- Take the inputs and save it in arrays.
- Add one more loop to find even numbers.
- Add system.out.println() function to print even.
At the end.
You learnt creating the Java program for Cube of numbers in arrays. So now enjoy the program.
Please comment on the post and share it.
And like it if you liked.