Write a program to find the given number is armstrong or not?
Write a program to find the given number is Armstrong or not in java?
An Armstrong number is a number that is equal to the sum of their individual digit cubes
Ex: 153 = (1*1*1 + 5*5*5 + 3*3*3)
Program:
Result :
Is 371 Armstrong number? = true
Is 523 Armstrong number? = false
Is 153 Armstrong number? = true
Tags:
Java ,
Java Interview Programs
An Armstrong number is a number that is equal to the sum of their individual digit cubes
Ex: 153 = (1*1*1 + 5*5*5 + 3*3*3)
Program:
package com.java.javafws.example; public class AmstrongEx { public boolean isArmstrongNumber(int number) { int tmp = number; int sum = 0; int div = 0; while (tmp > 0) { div = tmp % 10; sum += div * div * div; // or Math.pow(div, 3); tmp = tmp / 10; } if (number == sum) { return true; } else { return false; } } public static void main(String a[]) { AmstrongEx an = new AmstrongEx(); System.out.println("Is 371 Armstrong number? = " + an.isArmstrongNumber(371)); System.out.println("Is 523 Armstrong number? = " + an.isArmstrongNumber(523)); System.out.println("Is 153 Armstrong number? = " + an.isArmstrongNumber(153)); } }
Result :
Is 371 Armstrong number? = true
Is 523 Armstrong number? = false
Is 153 Armstrong number? = true
Get Updates
Subscribe to our e-mail to receive updates.
Related Articles
Subscribe to:
Post Comments (Atom)
0 Responses to “Write a program to find the given number is armstrong or not?”
Post a Comment
Thanks for your comments