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
⌛
This post has been reserved for your question.
Hey @Dhruuuuuuvv! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Please format your code to make it more readable. For java, it should look like this:
im curious as to why there is a method named "main" with the parameters of
(int n)
💤
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.
it works on bluej, I personally don't know how to answer that question 😅
but my real question isn't even answered
why not make a case for any numbers
not just 100 and 1000
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.
you could make a loop that grabs each digit and does the calculation for it
for example just
and the rest is fairly easy
is there a way to not use math library and still be able to execute the same operation as you have done here?
you could do
or
thanks mate!
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.
Post Closed
This post has been closed by <@729698616909758554>.