Example Program – Armstrong Number


Below sample programs finds whether the given number is an Armstrong Number or not.

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. Few more Examples for the Armstrong numbers are : 153, 371, 9474, 54748.

 

package in.javatutorials;

/**
* @author malliktalksjava.in
*
*/
public class ArmstrongNumber {

/**
* @param inputNumber
* @return boolean true/false will be return
*/
public static boolean isArmstrong(int inputNumber) {
String inputAsString = inputNumber + “”;
int numberOfDigits = inputAsString.length();
int copyOfInput = inputNumber;
int sum = 0;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
copyOfInput = copyOfInput / 10;
}
if (sum == inputNumber) {
return true;
} else {
return false;
}
}

/**
* @param args
*/
public static void main(String[] args) {
int inputNumber = 153;
System.out.print(“Enter a number: “);
boolean result = isArmstrong(inputNumber);
if (result) {
System.out.println(inputNumber + ” is an armstrong number”);
} else {
System.out.println(inputNumber + ” is not an armstrong number”);
}
}
}

 

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , ,
Posted in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
August 2013
M T W T F S S
« Jul   Oct »
 1234
567891011
12131415161718
19202122232425
262728293031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 284,813 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100