Given an array of numbers. For each number in array, find the multiples that exist in the array.
Eg
Array - [2, 3, 6, 10]
Output-
Multiples of 2 - 6,10
Multiples of 3 - 6
Multiples of 6 - No multiples
Multiples of 10 - No multiples

However I am getting

Multiples of each element of array is:
Multiples of2Is No multiples
Multiples of2is:6
Multiples of2is:10
Multiples of2is:0
Multiples of3is:6
Multiples of3Is No multiples
Multiples of3is:0
Multiples of6Is No multiples
Multiples of6is:0
Multiples of10is:0

What I have tried:

import java.util.*;
import java.io.*;
import java.lang.*;
publicclass multiplesarrays
{
publicstaticvoid main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no: of elements in array");
int n=sc.nextInt();
int arr[]=new int[100];
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Multiples of each element of array is:");
for(int i=0; i<n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(arr[j]%arr[i]!=0)
{
System.out.println("Multiples of"+arr[i]+ "Is No multiples");
}
elseif(arr[j]%arr[i]==0)
{
System.out.println("Multiples of"+arr[i]+"is:"+arr[j]);
}
}
}
}
}

As programmer, your job is also to find the bugs in your code.
- If you look carefully at the input, there is no duplicates and values are sorted. If you don't enforce those conditions, output can not be correct.

Quote:

Java

Multiples of2is:0

- This tells you that you are testing 2 against a value that is not in the input.
- How many times "Multiples of 2 - " is printed in correct result, how many times is it printed with your code ?
- When do you know the there is no multiple ? When is your code printing it ?

Ask yourself those question, the answers should lead your to spot problems and then craft corrections.

[edit]
This changed code may help you to understand what is going on.

Java

import java.util.*;
import java.io.*;
import java.lang.*;
publicclass multiplesarrays
{
publicstaticvoid main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no: of elements in array");
int n=sc.nextInt();
int arr[]=new int[100];
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Multiples of each element of array is:");
for(int i=0; i<n; i++)
{
System.out.println("i= "+i);for(int j=i+1; j<=n; j++)
{
System.out.println("j= "+j);if(arr[j]%arr[i]!=0)
{
System.out.println("Multiples of"+arr[i]+ "Is No multiples");
}
elseif(arr[j]%arr[i]==0)
{
System.out.println("Multiples of"+arr[i]+"is:"+arr[j]);
}
}
}
}
}

What if I have to bring my output in this format: Multiples of 2 - 6,10 Multiples of 3 - 6 Multiples of 6 - No multiples Multiples of 10 - No multiples How do we modify the code then?

What if I have to bring my output in this format: Multiples of 2 - 6,10 Multiples of 3 - 6 Multiples of 6 - No multiples Multiples of 10 - No multiples How do we modify the code then?

It is really not complicated,you should take advantage of this and train yourself to debug. At least, try some things in different places. Look at your result: you check every value in list against every remaining values in list + against 0 which is not in list. try to make checks 1 time fewer. When do you know that a value have no multiple ? After each check or something else ?

Programming is about being smart, start trainig now.

Simple solution: Sort the array. Then all duplicates are next to each other, and can be located, counted, and printed in a single pass.
Hint: Java has a sort method for arrays built in ...

But do yourself a favour and indent your code - it's a lot more readable that way.

[edit]
Just for kicks, I had 2 minutes to spare, so I tried this exercise in C# ...

C#

private Dictionary<int, IEnumerable<int>> GetMultiples(int[] arr)
{
Array.Sort(arr);
Dictionary<int, IEnumerable<int>> counts = new Dictionary<int, IEnumerable<int>>();
for (int i = 0; i < arr.Length; i++)
{
int val = arr[i];
counts[val] = arr.Where(x => x != val && x % val == 0);
}
return counts;
}
...
int[] arr = { 2, 3, 6, 10 };
Dictionary<int, IEnumerable<int>> counts = GetMultiples(arr);
foreach (int key in counts.Keys)
{
IEnumerable<int> values = counts[key];
int count = values.Count();
if (count == 0) Console.WriteLine($"{key} has no multiples");
else
{
Console.WriteLine($"{key} has {count} multiples: {string.Join(",", values.Select(n => n.ToString()))}");
}
}

Results

2 has 2 multiples: 6,103 has 1 multiples: 66 has no multiples
10 has no multiples

You can't use that code directly in Java, but at least it shows you it's possible ...
[/edit]

After sorting, I am getting this: Multiples of each element of array is: Multiples of3is:6 Multiples of3Is No multiples Multiples of3is:0 Multiples of3Is No multiples Multiples of6Is No multiples Multiples of6is:0 Multiples of6Is No multiples Multiples of10is:0 Multiples of10Is No multiples Exception in thread "main" java.lang.ArithmeticException: / by zero at multiplesarrays.main(multiplesarrays.java:35) I am getting this exception