Java Program to find Armstrong numbers with Example (original) (raw)

Armstrong number Example in Java
How to check if a number is an Armstrong number or not? or write a Java program to find Armstrong's number? This is a common Java interview question asked on-campus interviews and fresher-level interviews. This is also a popular Java programming exercise on various schools, colleges, and computer courses to build programming logic among Students. An Armstrong number is a 3 digit number for which the sum of cube of its digits is equal to the number itself.

One of the popular examples of the Armstrong number is 153 as 153= 1+ 125+27 which is equal to 1^3+5^3+3^3 which is equal to the sum of cube of its digits 1, 5, and 3

One more example of the Armstrong number is 371 because it is the sum of 27 + 343 + 1 which is equal to 3^3 + 7^3 + 1^3. In this Java program example, we will see a complete code example of a Java program to check if any 3 digit number is an Armstrong number or not.

If you are going for a Java interview, then be prepared for some follow-up questions like finding prime numbers, or finding Armstrong numbers of more than 3 digits.

Btw, good knowledge of essential programming concepts, operators, algorithms, data structure, and logic is important for any software engineer, particularly to do well on technical interviews. If you are preparing for a tech interview then you should double down on these topics.

If you need recommendations to refresh your Data Structure and algorithm skills then I highly recommend checking out Data Structures and Algorithms: Deep Dive Using Java course on Udemy. It's a hands-on course and covers all essential data structures. It's also very affordable and you can get in just $10 on Udemy flash sales which happen every now and then.

How to check if a number is Armstrong number in Java? Solved

Here is the complete code for checking if a number is an Armstrong number or not. It uses a method called isArmstrong(int number) to implement the logic for checking if a number is Armstrong nor not.

Btw, this is not the same program as print all Armstrong numbers between 0 and 999 but you can use this logic to solve that question as well.

All you need to do is loop till 1000 and check if the number is Armstrong or not. If yes then print otherwise move to the next number.

By the way, this Java program is in continuation of our earlier programming exercise like

These programs will help you to build the coding sense required to solve coding problems on interviews and your day-to-day life. It's also one of the important skills to map a new coding problem into an already known one and essential to pass the coding interview on companies like Google, Facebook, Amazon, and Microsoft.

If you are preparing for those companies then I also suggest you go through Grokking the Coding Interview: Patterns for Coding Questions on Educative, an interactive portal for coding interviews to learn some 16 useful coding patterns like Sliding Window, Two Pointers, Fast and Slow Pointers, Merge Intervals, Cyclic Sort, and Top K elements that can help you to solve zones of frequently asked coding problems.

How to check if a number is Armstrong number in Java

Java Program to Find Armstrong Number

import java.util.Scanner;

/**

}

Output: Please enter a 3 digit number to find if its an Armstrong number: 153 Number : 153 is an Armstrong number Please enter a 3 digit number to find if its an Armstrong number: 153 Number : 153 is an Armstrong number Please enter a 3 digit number to find if its an Armstrong number: 371 Number : 371 is an Armstrong number

How to find Armstrong number in Java with ExampleThat's all on How to check if a number is Armstrong in Java. It’s a pretty simple Java program and if you look closely it just gets digit by digit by using the remainder operator and reduces the number by 1 digit after dividing it by 10. Let me know if you find any bug on this Java program to check for the Armstrong number.

Other Programming Interview problems you may like to solve

Thanks for reading this article so far. If you find this logic-based Java coding problem from Google and my explanation useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are preparing for a programming job interview, then you must prepare for an all-important topic like data structure, String, array, etc. One course which can help you with this task is the Grokking the Coding Interview: Patterns for Coding Questions course. It contains popular coding interview patterns which will help you to solve most of the problems in your coding interviews.

By the way, What is your favorite Java coding exercise? Palindrome, Prime Number, Producer consumer problem , or this one? Do let me know in comments.