Write a program to find the numbers in an array which are powers of any other number present in the same array. It need not be square or cube, it could be any power greater than 1.
Input (int[]) | Output (int[]) |
---|---|
{2, 5, 6, 9, 16, 36, 50, 64, 81, 100, 128, 144} | {16, 36, 64, 81, 128} |
{3, 5, 7, 9, 15, 20, 25, 27, 55, 81, 100} | {9, 25, 27, 81} |
{2, 4, 5, 16, 20, 25, 55, 60, 100, 125} | {4, 16, 25, 125} |
{1, 6, 10, 36, 50, 100, 200, 500, 1000} | {36, 100, 1000} |
class FindNumbersPowerOfAnotherNumber
{ public static void main(String s[])
{
int inputArray[] = {2, 4, 5, 6, 8, 9, 16, 25, 64};
int output[] = getNumbers(inputArray);
System.out.println("Numbers which are powers of another number in same array are ");
for (int result : output)
System.out.print(result + ", ");
}
public static int[] getNumbers(int inputArray[]) {
//Write code here to get numbers which are powers of another number
}
//Write additional methods if required.
}
Topic: Learn Arrays And Loops