{Not a challenge}
WAP to store 15 numbers in an array then keep their unit digits in another array. Display THOSE numbers which STARTS with the digits kept in the second array.
Answers
Solution:
The given program is written in Java.
import java.util.*;
public class Array {
public static void main(String s[]) {
Scanner sc=new Scanner(System.in);
int ELEMENTS=15,i;
int numbers[]=new int[ELEMENTS];
int digits[]=new int[ELEMENTS];
for(i=0;i<ELEMENTS;i++){
System.out.print("a["+i+"] = ");
numbers[i]=sc.nextInt();
digits[i]=numbers[i]%10;
}
System.out.println("Given Array: "+Arrays.toString(numbers));
System.out.println("Last Digit of each numbers in the array - "+Arrays.toString(digits));
System.out.println("Required Numbers are - ");
for(int x:numbers){
if(ItemPresent(digits,startingDigit(x))){
System.out.print(x+" ");
}
}
}
static int startingDigit(int x){
for(;x>9;x/=10){}
return x;
}
static boolean ItemPresent(int a[],int x){
for(int y:a){
if(x==y)
return true;
}
return false;
}
}
Explanation:
- Array numbers[] stores the elements and digit[] stores the unit digits of each element of the array numbers[].
- startingDigit() returns the starting digit of any number and ItemPresent() checks whether an element is present in array or not.
- A loop iterates through the elements of array numbers[]. Now, if the starting digit of the number starts with the element kept in array digits[], then it displays the number.
See the attachment for output.