Create a java program that use\'s nested forloops to find all the three-digit Ar
ID: 3681785 • Letter: C
Question
Create a java program that use's nested forloops to find all the three-digit Armstrong numbers. Armstrong numbers are three digit numbers such that the sum of the digits cubed is equal to the number itself.
For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 153
However, 294 is not, because 2³ + 9³ + 4³ = 801 (not 294).
In addition, your program should count how many multiplication operations the program performed. If your number of multiplication operations are bigger than mine, please think about how to reduce it, and try to modify your program to get the smallest number of multiplication operations.
The output should be like the following:'
output output demo (runl x run All three digit Armstrong Numbers are the following: (1 1 1 5* 5*5 3* 3* 3) 1.53 (3* 3* 3 7 7* 7 0 0 0) 370 (3* 3* 3 7 7* 7 1* 1* 1) 371 (4 4 4 0 0 0 7* 7* 7) 07 The program performed 2097 multiplication operationss BUTID SUCCESSFUL (tota time 0 seconds)Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class ArmStrongNum
{
public static void main (String[] args) throws java.lang.Exception
{
int Num=0,ArgStr=0,cnt=0;
for(int i=1;i<=9;i++){
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++){
ArgStr = (i*i*i )+(j*j*j)+(k*k*k);
Num = i*100+j*10+k;
if(Num==ArgStr)
System.out.println("( "+i+" * "+i+" * "+i+" ) + ( "+j+" * "+j+" * "+j+" ) + ("+k+" * "+k+" * "+k+" ) = "+ArgStr);
cnt++;
}
}
}
System.out.println("The program performed "+cnt+" multiplication operationss.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.