In the previous article, we have seen Java Program to Replace Each Element of the Array by the Product of its Next Element
In this article we are going to see how to find the single digit array elements using Java programming language.
Java Program to Find the Single Digit Array Elements
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find the single digit array elements.
Method-1: Java Program to Find the Single Digit Array Elements By Using Division By 10
Approach:
Any single digit variable of int
data type would give 0 as result on performing a division with 10.
- Create scanner class object.
- Ask user for the length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Iterate over the array.
- If
arr[i] / 10 == 0
, print the element.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //calling the method findSingleDigitElement() findSingleDigitElements(arr); } //findSingleDigitElement() method to find all the single digit elemnt in the array public static void findSingleDigitElements(int[] arr) { System.out.println("Single digit elements are: "); // iterate through the array for (int i = 0; i < arr.length; i++) { // check if the element is single digit if (arr[i] / 10 == 0) { System.out.print(arr[i] + " "); } } } }
Output: Enter the size of array: 8 Enter array elements: 56 9 1213 3 34 5 8 345 Single digit elements are: 9 3 5 8
Method-2: Java Program to Find the Single Digit Array Elements By Checking -9 to 9
Approach:
- Create scanner class object.
- Ask user for the length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Iterate over the array.
- If the element lies in between -9 to 9 then print it as it is single digit element.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //calling the method findSingleDigitElement() findSingleDigitElements(arr); } //findSingleDigitElement() method to find all the single digit elemnt in the array public static void findSingleDigitElements(int[] arr) { System.out.println("Single digit elements are: "); // iterate through the array for (int i = 0; i < arr.length; i++) { // check if the element is single digit if (arr[i] >= -9 && arr[i] <= 9) { System.out.print(arr[i] + " "); } } } }
Output: Enter the size of array: 10 Enter array elements: 78 -5 342 -4 8 22 890 4 9 4567 Single digit elements are: -5 -4 8 4 9
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs:
- Java Program to Delete All the Even Elements from the Array of Integers
- Java Program to Delete All the Odd Elements from the Array of Integers
- Java Program to Find all Elements in Array which have at-least Two Greater Elements
- Java Program to Find all Elements in Array which have at-least Two Smaller Elements