Armstrong numbers

An Armstrong(after Michael F. Armstrong) number, also known as Narcissistic number or pluperfect digital invariant(PPDI) OR plus perfect number, is a number that is sum of its own digits each raised to the power of the number of digits.
For example:
If the number is 371. How to find this number is an Armstrong number. First check the number of digit in this number. There are 3 digit, so take 3 as power.
Now,  33=27
73=343
13 = 1
27+343+1=371
1634 is Armstrong or not?
There are 4 digit in 1634 .So, take power as 4.
And we get, 14+64+34+44=1634
1634 is an Armstrong number

A simple Java program to find a three digit number is Armstrong or not
 // AMSTRONG NUMBER OF THREE DIGIT
 import java.io.*;  
 class Myamst{  
 public static void main(String arg[])throws IOException  
 {  
 int a=0,e=0,s=0,am=0;  
 DataInputStream dip=new DataInputStream(System.in);  
 System.out.println("\n");  
 System.out.println("\t*-- PROGRAM TO CHECK THE THREE DIGIT NUMBER IS AMSTRONG OR NOT --*");  
 System.out.print("\t");  
 for(int i=0;i<18;i++)  
 {System.out.print("_ _");}  
 System.out.println("\n");  
 System.out.print("ENTER THE THREE DIGIT NUMBER = ");  
 a=Integer.parseInt(dip.readLine());  
 s=a;  
 while(s>0)  
 {  
 e=s%10;  
 am=am+(e*e*e);  
 s=s/10;  
 }  
 if(a==am)  
 System.out.print("Number is Amstrong");  
 else  
 System.out.println("Number is not Amstrong");  
 }  
 }  
OUTPUT:
A simple Java program to find any number Armstrong or not
 //AMSTRONG NUMBER  
 import java.io.*;  
 class Myamsta{  
 public static void main(String arg[])throws IOException  
 {  
 int a=0,e=0,s=0,count=0;  
 double am=0;  
 DataInputStream dip=new DataInputStream(System.in);  
 System.out.println("\n");  
 System.out.println("\t*-- PROGRAM TO CHECK NUMBER IS AMSTRONG OR NOT --*");  
 System.out.print("\t");  
 for(int i=0;i<17;i++)  
 {System.out.print("_ _");}  
 System.out.println("\n");  
 System.out.print("ENTER THE NUMBER = ");  
 a=Integer.parseInt(dip.readLine());  
 s=a;  
 while(s>0)  
 {  
 e=s%10;  
 count++;  
 s=s/10;  
 }  
 e=0;s=0;  
 s=a;  
 while(s>0)  
 {  
 e=s%10;  
 am=am+Math.pow(e,count);  
 s=s/10;  
 }  
 if(a==am)  
 System.out.print("Number is Amstrong");  
 else  
 System.out.println("Number is not Amstrong");  
 }  
 }  
OUTPUT:
Program to print Armstrong numbers between two limits
 //AMSTRONG NUMBER BETWEEN LIMITS  
 import java.io.*;  
 class Srcamsta{  
 public static void main(String arg[])throws IOException  
 {  
 int a=0,s=0;  
 int n=0,i,c,flag=0;  
 double e=0,ams=0,count=0;;  
 DataInputStream dip=new DataInputStream(System.in);  
 //UI  
 System.out.println("\n");  
 System.out.println("\t*-- PROGRAM TO FIND AMSTRONG NUMBERs --*");  
 System.out.print("\t");  
 for( i=0;i<14;i++)  
 {System.out.print("_ _");}  
 System.out.println("\n");  
 //Reading limits  
 System.out.print("ENTER LOWER LIMIT= ");  
 a=Integer.parseInt(dip.readLine());  
 System.out.print("ENTER THE UPPER LIMIT= ");  
 n=Integer.parseInt(dip.readLine());  
 //Search process  
 //  
 System.out.println("Amstrong numbers between "+a+" and "+n+" are");  
 for(c=a+1 ;c<n;c++)  
 {  
 ams=0;  
 count=0;//count initialised to zero who holds number of digits in the digit  
 s=c;  
 while(s>0)  
 {  
 e=s%10;  
 count++;//counting the digits  
 s=s/10;  
 }  
 e=0;  
 s=c;  
 while(s>0)  
 {  
 e=s%10;  
 ams=ams+Math.pow(e,count);  
 s=s/10;  
 }  
 //printing  
 if(ams==c)  
 {  
 System.out.println(c+" ");  
 flag=1;}  
 }  
 if(flag==0)  
 System.out.println(" There are none!!");  
 }  
 }  
OUTPUT:

No comments: