Question
print elements in an array divisible by number given by user.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class ElementsInArrayDivisibeByGivenNumber
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;
int n=0,i=0,j=0,count=0,div;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number of elements in the array");
n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the divisor");
div = sc.nextInt();
/* printing elements in an array divisible by 'div' */
System.out.println("Elements in an array divisible by "+div);
for(i=0;i< n;i++)
{
if(arr[i]%div==0)
{
System.out.print(arr[i]+" ");
}
}
}
}