Java Tutorial
Armstrong number is a number which is equa to sum of digits raise to the power of total number of digits in the number.
Let us consider 153, here 3 digits in number.
1^3+5^3+3^3 => 1+125 +27 => 153 which is same as number, so 153 is an Armstrong number.
Some Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774
Also known as narcissistic numbers.
This java program checks if a number is Armstrong or not.
Armstrong numbers are the sum of their own digits to the power of the number of digits.
import java.util.Scanner; public class ArmstrongNumber { public static int power(int number, int pow){ int powerVal = 1; int i=1; while(i<=pow) { powerVal *= number; i++; } return powerVal; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Checks Armstrong Number"); System.out.println("------------------------"); System.out.print("Number: "); int number = in.nextInt(); int temp = number; int digits=0, remainder, sum=0; while(temp != 0) { digits++; temp = temp /10; } temp = number; while(temp!=0){ remainder = temp % 10; sum = sum + power(remainder, digits); temp = temp/10; } if (number == sum){ System.out.println("Number '"+number+"' is an Armstrong Number!"); } else { System.out.println("Number '"+number+"' is not an Armstrong Number!"); } } }Output:
D:\Java_Programs>javac ArmstrongNumber.java D:\Java_Programs>java ArmstrongNumber Checks Armstrong Number ------------------------ Number: 153 Number '153' is an Armstrong Number! D:\Java_Programs>javac ArmstrongNumber.java D:\Java_Programs>java ArmstrongNumber Checks Armstrong Number ------------------------ Number: 151 Number '151' is not an Armstrong Number!
import java.util.Scanner; public class ArmstrongNumber { public static int power(int number, int pow){ int powerVal = 1; for(int i=1; i<=pow; i++) { powerVal *= number; } return powerVal; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Checks Armstrong Number"); System.out.println("------------------------"); System.out.print("Number: "); int number = in.nextInt(); int digits=0, remainder, sum=0; for(int temp=number; temp != 0; temp=temp/10) { digits++; } for(int temp = number; temp!=0; temp=temp/10){ remainder = temp % 10; sum = sum + power(remainder, digits); } if (number == sum){ System.out.println("Number '"+number+"' is an Armstrong Number!"); } else { System.out.println("Number '"+number+"' is not an Armstrong Number!"); } } }Output:
D:\Java_Programs>javac ArmstrongNumber.java D:\Java_Programs>java ArmstrongNumber Checks Armstrong Number ------------------------ Number: 370 Number '370' is an Armstrong Number! D:\Java_Programs>javac ArmstrongNumber.java D:\Java_Programs>java ArmstrongNumber Checks Armstrong Number ------------------------ Number: 372 Number '372' is not an Armstrong Number!
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page