write a program to initialize a SDA of 10 numbers and store the numbers which are ending with 2 in to a new SDA. display the new SDA.
help
Answers
Answered by
1
Solution.
The given co de is written in Java.
import java.util.Arrays;
public class ArrayOperation{
public static void main(String args[]){
int a[]={1,2,4,29,92,62,34,78,69,102};
System.out.println("Given Array: "+Arrays.toString(a));
System.out.print("Numbers ending with 2 from the array are as follows - ");
for(int i:a){
if(i%10==2)
System.out.print(i+" ");
}
}
}
Explanation.
- Here, we have initialized an array of ten numbers.
- Then we have to transverse through array elements. The last digit is obtained by modulo operation i.e., the remainder obtained by dividing the number by 10. If the last digit is 2, display it.
See attachment for output.
Attachments:
Similar questions