GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Java Program to Check Armstrong Number or Not
Java Program to Check Armstrong Number or NotThis article covers multiple programs in Java that checks whether a number entered by user at run-time of the program, is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its own digits, where each digit raised to the power of number of digit. For example, 153 is an Armstrong number, because: 153 = 13 + 53 + 33
= 1 + 125 + 27
= 153
Some other Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 370, 371, 407, 1634 etc. Check Armstrong Number in Java using while LoopThe question is, write a Java program to check whether a number is an Armstrong number or not. The number must be received by user at run-time. The program given below is its answer: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int num, temp, totalDigit=0, res=0, rem, pow, i; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); temp = num; while(num>0) { num = num/10; totalDigit++; } num = temp; while(num>0) { rem = num%10; pow = 1; i = 0; while(i<totalDigit) { pow = pow*rem; i++; } res = res + pow; num = num/10; } if(res==temp) System.out.println("\nArmstrong Number"); else System.out.println("\nNot an Armstrong Number."); } } The snapshot given below shows the sample run of above program with user input 407 as number to check whether it is an Armstrong number or not: The above program can also be written as. This program uses Math.pow() method to directly calculate the value, of a number to the power of another number: import java.util.Scanner; import java.lang.Math; public class CodesCracker { public static void main(String[] args) { int num, temp, totalDigit=0, res=0, rem; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); temp = num; while(num>0) { num /= 10; totalDigit++; } num = temp; while(num>0) { rem = num%10; res = res + (int) Math.pow(rem, totalDigit); num /= 10; } if(res==temp) System.out.println("\nArmstrong Number"); else System.out.println("\nNot an Armstrong Number."); } } The sample run of above program with user input 371 is shown in the snapshot given below: Check Armstrong Number in Java using for LoopThe previous program, that was created using while loop, can also be created using for loop, as shown here: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int num, temp, totalDigit=0, res=0, rem; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); for(temp=num; num>0; num /= 10) totalDigit++; for(num=temp; num>0; num /= 10) { rem = num%10; res = res + (int) Math.pow(rem, totalDigit); } if(res==temp) System.out.println("\nArmstrong Number"); else System.out.println("\nNot an Armstrong Number."); } } Check Armstrong Number in Java - Only for 3-Digit NumberHere is another way, that we can create the same program. But this program is limited to 3-digit number only: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int num, temp, res=0, rem; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); temp = num; while(temp!=0) { rem = temp%10; res = res + (rem*rem*rem); temp = temp/10; } if(num==res) System.out.println("\nArmstrong Number."); else System.out.println("\nNot an Armstrong Number."); } } Same Program in Other Languages
« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |