__Armstrong number Example in Java__How to check if a number is Armstrong number or not or write a Java program to find Armstrong number is common Java interview question asked on campus interviews or fresher level interview. This is also a popular Java programming exercise on various school and colleges. An Armstrong number is a 3 digit number for which sum of cube of its digit is equal to the number. Example of Armstrong number is 153 as 153= 1+ 125+27 which 1^3+5^3+3^3. Another Armstrong number is 371. In this Java program example we will see complete code example of Java program to check if any 3 digit number is Armstrong number or not. If you are going for Java interview, then be prepare for some follow question e.g. finding prime numbers, or finding Armstrong number of more than 3 digit.

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

If you haven't read them already you may find them useful as programming exercise or interview question.

##

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

That's all on

Other Java programming problems you may like :

- Java program to reverse String in Java,
- Java program to reverse number in Java and
- Java program to print Fibonacci number in Java.
- Program for calculating factorial of a number using recursion in Java.

If you haven't read them already you may find them useful as programming exercise or interview question.

##
__How to check if number is Armstrong number in Java__

Here is complete code for checking if a number is Armstrong number or not. It uses a method called isArmstrong(int number) to implement logic for checking number as Armstrong.import java.util.Scanner; /** * Simple Java Program to check or find if a number is Armstrong number or not. * An Armstrong number of three digit is a number whose sum of cubes of its digit is equal * to its number. For example 153 is an Armstrong number of 3 digit because 1^3+5^3+3^3 or 1+125+27=153 * @author Javin */ public class ArmstrongTest{ public static void main(String args[]) { //input number to check if its Armstrong number System.out.println("Please enter a 3 digit number to find if its an Armstrong number:"); int number = new Scanner(System.in).nextInt(); //printing result if(isArmStrong(number)){ System.out.println("Number : " + number + " is an Armstrong number"); }else{ System.out.println("Number : " + number + " is not an Armstrong number"); } } /* * @return true if number is Armstrong number or return false */ private static boolean isArmStrong(int number) { int result = 0; int orig = number; while(number != 0){ int remainder = number%10; result = result + remainder*remainder*remainder; number = number/10; } //number is Armstrong return true if(orig == result){ return true; } return false; } } 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

That's all on

**How to check if a number is Armstrong in Java**. It’s pretty simple Java program and if you look closely it just gets digit by digit by using remainder operator and reduce number by 1 digit after dividing it by 10. Let me know if you find any bug on this Java program for checking Armstrong number.Other Java programming problems you may like :

**Related topics:**

Good tutorial, How to find Armstrong number with more than 3 digits ?

ReplyDelete/*

DeleteSimple Java Program to check or find if a number is Armstrong number or not.

[ An Armstrong number of any number of digit is a number whose sum of each digits raised to the power the total no. of digits is equal to it (the original number). For example, 9474 is an Armstrong number of 4 digits because 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 = 9474 ]

*/

import java.io.*;

import java.util.*;

public class ArmstrongNumber

{

public static void main(String args[])

{

// Input number to check if its Armstrong number

System.out.println("Please enter a number to find if it is an Armstrong number: ");

int number = new Scanner(System.in).nextInt();

// Printing result

if(isArmStrong(number))

{

System.out.println(number + " is an Armstrong number");

}

else

{

System.out.println(number + " is not an Armstrong number");

}

} // End of main()

// Function returning true if number is Armstrong number or returning false

private static boolean isArmStrong(int number)

{

int result = 0;

int dc=0;

int orig = number;

while(orig != 0)

{

dc++;

orig = orig/10;

}

orig = number;

while (orig!=0)

{

result = result + (int)Math.pow(orig%10, dc);

orig = orig/10;

}

if(number == result)

return true;

else

return false;

} // End of isArmStrong()

}

OUTPUT:

Please enter a number to find if it is an Armstrong number:

9474

9474 is an Armstrong number

Please enter a number to find if it is an Armstrong number:

8208

8208 is an Armstrong number

package com.example.test.nover;

Deleteimport java.util.List;

import java.util.ArrayList;

public class ArmstrongNumber {

public void findNumber() {

List numList = new ArrayList();

int newNum = 0;

String numVal= "";

int length = 0;

for(int i = 1000; i<=9999; i++) {

numVal = (numVal+i).trim();

length = numVal.length();

for(int j=0;j<length;j++) {

char charVal = numVal.charAt(j);

int charNum = charVal-'0';

charNum = (int) Math.pow(charNum,4);

//int charNo = (int) Math.pow(charNum,4);

newNum = newNum + charNum;

}

if(i==newNum)

numList.add(i);

newNum = 0;

numVal="";

}

for(int i: numList) {

System.out.println("NUMBERS FOUND ARE " +i);

}

}

public static void main(String args[]) {

ArmstrongNumber numVer = new ArmstrongNumber();

numVer.findNumber();

}

}

Using String will generalize to find ArmStrong Number for 2 digits, 3 digits or 4 digits since we can capture the length and use Math.pow(number,length) which will make the code generic.

DeleteAccording to Abhirupa Mitra's code numbers from 1 to 9 would be displayed as Armstrong numbers which is not correct. What is the correct definition of Armstrong numbers? Within 1 to 10 only 1 is an armstrong number.

ReplyDeleteYes, you are right. The correct definition of Armstrong number is:-

Deleteex. 153. if 1^3+5^3+3^3 = 153 then it is an Armstrong number. In words, a number whose sum is equal to the sum of the cubes of its digits then it is an Armstrong number

The definition of an Armstrong Number was not that clear to me. I looked up here and figured out what it is: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html

ReplyDeleteI'd probably do it like this:

ReplyDeletepublic boolean isArmstrong(int i) {

return (i >= 100) && (i <= 999) && (sumCubeDigits(i) == i);

}

private int sumCubeDigits(int digits) {

if (digits == 0)

return 0;

else

return cube(digits % 10) + sumCubeDigits(digits / 10);

}

private int cube(int i) {

return i * i * i;

}

and if it turned out to be too slow, like this:

public boolean isArmstrong(int i) {

if ((i < 100) || (i > 999)) {

return false;

} else {

int a = i % 10;

int b = (i / 10) % 10;

int c = i / 100;

int sumOfCubes = (a*a*a) + (b*b*b) + (c*c*c);

return (i == sumOfCubes);

}

}

I'd do it like this....

ReplyDeletepublic static void armstrong(int number){

String numToString = String.valueOf(number);

int number1;

int multiplication = 0;

for (int i=0; i<=numToString.length()-1; i++){

number1 = Character.getNumericValue(numToString.charAt(i));

multiplication = multiplication + number1 * number1 * number1;

}

if (number == multiplication) {

System.out.println("Number : " + number + " is an Armstrong number");

} else {

System.out.println("Number : " + number + " is not an Armstrong number");

}

}

I found this program interesting and quite simple to understand! They have explained few elements on their page which helped me to grasp the topic.'

ReplyDeleteIts better to use a single function t understand the Java program better.

import java.io.*;

public class armstrong

{

public static void main(String args[]) throws Exception

{

int num,temp,sum=0,rem=0;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("\nEnter an Integer:");

num=Integer.parseInt(br.readLine());

temp=num;

while(num>0)

{

rem=num%10;

sum=sum+(rem*rem*rem);

num=num/10;

}

if(temp==sum)

{

System.out.println("\n" + temp + " is an Armstrong Number\n");

}

else

{

System.out.println("\n" + temp + " is Not an Armstrong Number\n");

}

}

}

Source: http://www.codingalpha.com/armstrong-number-java-program/