Is this Armstrong number checking code perfect for 3 digits?

public class wow { public static void main(int n) { if (n>=100 && n<1000) { System.out.println("Number selected: "+n); int a=n%10; int b=n/100; int c=(n/10)%10; int d=(aaa)+(bbb)+(ccc); if (d == n) { System.out.println("Armstrong"); } else { System.out.println("Not Armstrong"); } } else { System.out.println("Invalid choice - pls enter a number from 100 to 999"); } } } This is my code. Can anyone pls clarify and tell me what changes I can make to make the code much better?
13 Replies
JavaBot
JavaBot•3w ago
⌛ This post has been reserved for your question.
Hey @Dhruuuuuuvv! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot•3w ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
j
j•3w ago
im curious as to why there is a method named "main" with the parameters of (int n)
JavaBot
JavaBot•3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Dhruuuuuuvv
DhruuuuuuvvOP•3w ago
it works on bluej, I personally don't know how to answer that question 😅 but my real question isn't even answered
ayylmao123xdd
ayylmao123xdd•3w ago
why not make a case for any numbers not just 100 and 1000
Dhruuuuuuvv
DhruuuuuuvvOP•3w ago
That's my next question - can anyone give me the logic for accepting any number from 1 to the max int range to check if it's armstrong or not just the logic, not the actual code.
ayylmao123xdd
ayylmao123xdd•3w ago
you could make a loop that grabs each digit and does the calculation for it for example just
while (number > 0) {
result += Math.round(Math.pow(number % 10, 3));
number /= 10;
}
while (number > 0) {
result += Math.round(Math.pow(number % 10, 3));
number /= 10;
}
and the rest is fairly easy
Dhruuuuuuvv
DhruuuuuuvvOP•3w ago
is there a way to not use math library and still be able to execute the same operation as you have done here?
ayylmao123xdd
ayylmao123xdd•3w ago
you could do
result += (number % 10) * (number % 10) * (number % 10);
result += (number % 10) * (number % 10) * (number % 10);
or
long digit = number % 10;
result += digit * digit * digit;
long digit = number % 10;
result += digit * digit * digit;
Dhruuuuuuvv
DhruuuuuuvvOP•3w ago
thanks mate!
JavaBot
JavaBot•3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot•3w ago
Post Closed
This post has been closed by <@729698616909758554>.

Did you find this page helpful?