Is number an Armstrong number - Java Algorithm

Java examples for Algorithm:Number

Introduction

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Demo Code


import java.*;/*  w  w  w  .j a  va 2  s.c  o  m*/
import java.io.*;
import java.util.*;

public class ArmstrongNumber {

  public static int cube(int n) {
    return n * n * n;

  }

  public static boolean isArm(int n) {

    int m = n;
    int c = 0;
    int temp = m;
    while (m > 0) {
      int a = m % 10;
      m = m / 10;
      int mCube = cube(a);

      c = c + (mCube);

    }

    if (temp == c) {
      return true;

    }

    else
      return false;

  }

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

    // Write code here

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter no to be Check : ");
    int mInput = scan.nextInt();
    boolean result = isArm(mInput);

    if (result == true) {
      System.out.println("True");
    } else
      System.out.println("False");

  }
}

Related Tutorials