Logic for Armstrong number :)
Armstrong Number
An Armstrong number is a positive m-digit number that
is equal to the sum of the mth powers of their digits. It is
also known as pluperfect, or Plus Perfect, or Narcissistic number.
It is an OEIS sequence A005188. Let’s understand it through an
example.
Armstrong Number Example
1: 11 = 1
2: 21 = 2
3: 31 = 3
153: 13 +
53 + 33 = 1 + 125+ 27 = 153
125: 13 +
23 + 53 = 1 + 8 + 125 = 134 (Not an
Armstrong Number)
1634: 14 +
64 + 34 + 44 = 1 + 1296 + 81 +
256 = 1643
//Armstrong number list:0,1,153,370,371,407,1634
//Eg 153 rest to 3(Total number of digit ) each digit if addition come original number.
package javaPrograms;
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int number = sc.nextInt();
int t1=number;
int length = 0;
while(t1!=0) {
t1 = t1/10;
length = length+1; // for find out number of digits
}
int t2 = number;
int arm = 0;
int rem ;
while(t2!=0) {
int mul =1;
rem = t2%10;
for(int i=1;i<=length;i++) {
mul = mul*rem;
}
arm = arm+mul;
t2 = t2/10;
}
if(arm==number) {
System.out.println(number +"is Armstrong number");
}
else {
System.out.println(number +"is not Armstrong number");
}
}
}
Comments
Post a Comment