Thursday, September 20, 2018

Armstrong Number or Not Java Program

Java program to check if a given number is an Armstrong number or not can be termed as a fresher level interview question. An Armstrong number is a number that is equal to the sum of the digits in a number raised to the power of number of digits in the number.

As Example - If we take 371, it is an Armstrong number as the number of digits here is 3, so

371 = 33 + 73 + 13 = 27 + 343 + 1 = 371

Another Example is 9474, here the number of digits is 4, so

9474 = 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9474

And obviously 0 and 1 are also Armstrong number.

Check given number Armstrong number or not

So let's write a Java program to check whether a given number is an Armstrong number or not. How does the program work is explained after the program.

import java.util.Scanner;

public class ArmstrongNumber {
  public static void main(String[] args) {
    System.out.println("Please enter a number : ");
    Scanner scanIn = new Scanner(System.in);
    int scanInput = scanIn.nextInt();
    boolean isArmstrong = checkForArmstrongNo(scanInput);
    if(isArmstrong){
     System.out.println(scanInput + "  is an Armstrong number");
    }else{
     System.out.println(scanInput + " is not an Armstrong number"); 
    }
    scanIn.close();
  }
 
  private static boolean checkForArmstrongNo(int number){
    // convert number to String
    String temp = number + "";
    int numLength = temp.length();
    int numCopy = number;
    int sum = 0;
    while(numCopy != 0 ){
      int remainder = numCopy % 10;
      // using Math.pow to get digit raised to the power
      // total number of digits
      sum = sum + (int)Math.pow(remainder, numLength);
      numCopy = numCopy/10;
    }
    System.out.println("sum is " + sum );
    return (sum == number) ? true : false;
  }
}

Some outputs-

Please enter a number : 
125
sum is 134
125 is not an Armstrong number

Please enter a number : 
371
sum is 371
371  is an Armstrong number

Please enter a number : 
54748
sum is 54748
54748  is an Armstrong number

Armstrong number Java program explanation

Here the input is taken from the user, that number is converted to String just to get the length of the number. Logic here is to get one digit of the number at a time, starting from the last digit, get the value of that number power raised to the number of the digits and then divide the number by 10 to reduce the number by one digit.
Repeat the process for all the digits in the given number. Keep adding the values to get the final result. Compare the result with the original number to check whether given number is Armstrong number or not.

That's all for this topic Armstrong Number or Not Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Check Whether Given String/Number is a Palindrome or Not
  2. How to Display Pyramid Patterns in Java
  3. Displaying Prime Numbers - Java Program
  4. Factorial program in Java
  5. Writing file in Java

You may also like-

  1. Finding Duplicate Elements in an Array - Java Program
  2. Difference Between Two Dates - Java Program
  3. How to Create Password Protected Zip File in Java
  4. Spring Example Program Using Automatic Configuration
  5. Java Collections Interview Questions
  6. abstract Class in Java
  7. Switch-Case Statement in Java
  8. Java SynchronousQueue With Examples

No comments:

Post a Comment