Advertisement
javatechie

ArmstrongNumber

Aug 28th, 2020
1,645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. package com.javatechie.interview.programming;
  2.  
  3. public class ArmstrongNumber {
  4.     /**
  5.      * 153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
  6.      **/
  7.     public static boolean isArmstrongNumber(int no) {
  8.         int temp = 0;
  9.         int sum = 0;
  10.         int original = no;
  11.         while (no != 0) {
  12.             temp = no % 10;
  13.             sum = (int) (sum + Math.pow(temp, 3));
  14.             no = no / 10;
  15.         }
  16.         return sum == original ? true : false;
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         System.out.println(isArmstrongNumber(371));
  21.     }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement