Q.Write a java Program to accept ‘n’ no’s through command line and store only armstrong no’s into the array and display that array. [15 M]
class ArmstrongArray
{
// Method to check Armstrong number
static boolean isArmstrong(int num)
{
int temp = num;
int digits = 0;
// Count digits
while(temp != 0)
{
digits++;
temp = temp / 10;
}
temp = num;
int sum = 0;
// Calculate Armstrong sum
while(temp != 0)
{
int rem = temp % 10;
sum += Math.pow(rem, digits);
temp = temp / 10;
}
return sum == num;
}
public static void main(String args[])
{
int arm[] = new int[args.length];
int count = 0;
// Read numbers from command line
for(int i = 0; i < args.length; i++)
{
int num = Integer.parseInt(args[i]);
if(isArmstrong(num))
{
arm[count] = num;
count++;
}
}
// Display Armstrong numbers array
System.out.println("Armstrong numbers are:");
for(int i = 0; i < count; i++)
{
System.out.print(arm[i] + " ");
}
}
}