Kamis, 16 Oktober 2014

Write a Program to Find all Armstrong number in the range of 0 and 9999 - Example



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, 153 is an Armstrong number, since 1**3 + 5**3 + 3**3 = 153, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Along with usual beginner exercises e.g. calculating factorial, reversing string or calculating prime numbers, this is a good exercise to build programming logic. It teaches you basic programming technique of how to use operator for something which is not obvious, for example, to solve this programming challenge, we first need to check if a number is Armstrong or  not, and to do this we need individual digits of the number. how do we do that? well there is a programming technique, which you might have learned while doing number palindrome exercise. If you modulo an integer number by 10, you will get last digit, for example 656%10 will give you 6, which is last digit of 656. Similarly to reduce the input after each iteration, you can use division operator, as 656/10 will return 65, which is without last digit. If you know this trick, it's very easy to solve any programming problem, which requires examination of individual digits.  This Java program uses same technique and compute all Armstrong numbers in the range of 0 and 999. By the way this program has different variations as well e.g. how do you find Armstrong number of four digit, as this program only calculates three digit Armstrong number. Well, for that you need to remember general definition of Armstrong number which is, An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. So a four digit Armstrong number will be equal to sum of power four of individual digits of that number. Another interesting challenge is to write  a program which uses recursion to find if number is Armstrong or not.








Armstrong Number Code Example in Java



How to check Armstrong number in Java with Example

Here is our Java program to display all Armstrong number between 0 and 9999. Actually there are only three digit Armstrong number in that range. Our solution is simple but general, we have a loop which runs up-to a number entered by user. So if user wants to see Armstrong number between 0 and 9999, he should enter 9999. Though it has one shortcoming, logic of checking if number is Armstrong or not is hard-coded to find only three digit numbers. So, precisely this is a program to display thee digit Armstrong number between 0 to 9999 or any user supplied upper range. Coming back to logic, all it does is :


  • Extract individual digits of number in each iteration 

  • Calculate cube of that digit and add into sum which is initialized with zero

  • reduce the number by factor of 10 to remove one digit.




It repeats this process until input is not zero, which is our base case to stop checking. At the end of this loop if calculated sum is equal to original number, then its an Armstrong other wise its not. This logic is encapsulated inside a private static method called isArmstrongNumber(int number). This is again called in a loop to supply all the numbers from 0 to 9999. Logic is simple but presents a powerful technique to solve any problem which is based in individual digit of number.



import java.util.Arrays;
import java.util.Scanner;

/**
* This Java program computes all Armstrong numbers in the range of 0 and 9999. An
* Armstrong number is a number such that the sum of its digits raised to the
* third power is equal to the number itself. For example, 153 is an Armstrong
* number, since 1**3 + 5**3 + 3**3 = 153.
*
* @author Javin Paul
*/

public class ArmstrongNumberDemo{

public static void main(String args[]) {

Scanner cmd = new Scanner(System.in);
System.out.println("Please enter a number up-to which Armstrong number will be find");
int count = cmd.nextInt();
int index = 0;
for (int i = 0; i < count; i++) {
if (isArmstrongNumber(i)) {
System.out.printf("Armstrong number %d: %d %n", index, i);
index++;
}

}
cmd.close();
}

/**
* Java Method to check if given number is Armstrong Number or not
*
* @param number
* @return true, if Armstrong number, false otherwise.
*/

public static boolean isArmstrongNumber(int number) {
int sum = 0;
int copyOfInput = number;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum += (lastDigit * lastDigit * lastDigit);
copyOfInput /= 10;
}

if (sum == number) {
return true;
}
return false;
}

}

Output
Please enter a number up-to which Armstrong number will be find
9999
Armstrong number 0: 0
Armstrong number 1: 1
Armstrong number 2: 153
Armstrong number 3: 370
Armstrong number 4: 371
Armstrong number 5: 407





That's all about how to find Armstrong number in Java. As I said this program is very popular coding exercise for Java beginners and there are lot of versions exists e.g. writing a program to check if given number is Armstrong or not, that could be any digit long so you need to write logic which can check it correctly. Similarly, there is one more version exist, writer program to print Armstrong number of four or five digits. Core logic of checking if a number is Armstrong or not is same, but you need to tweak them little bit to solve these programming problems. Apart from this, I would recommend following programs to any Java beginners :



1.Write a Java Program to See if two String are Anagram of each other? (Solution)
2. How to count occurrences of  a character in String? (Solution)
3. How to find first non repeated characters from String in Java? (See here for solution)
4. Write a Program to check if a number is binary in Java? (Solution)
5. How to remove duplicates from array without using Collection API? (Solution)
6. Write a Program to calculate Sum of Digits of a number in Java? (Solution)


7. Write a Program to prevent Deadlock in Java? (Click here for solution)
8. Write a Program to solve Producer Consumer Problem in Java. (Solution)
9. How to reverse String in Java without using API methods? (Solution)
10. How to check if Array contains duplicate number or not? (Solution)
11. How to remove duplicates from ArrayList in Java? (Solution)


























Source:http://javarevisited.blogspot.com/2014/07/write-program-to-find-all-armstrong-number-between-0-to-9999.html

Tidak ada komentar:

Posting Komentar