Armstrong number
----------------------
Armstrong number is a number that is the sum of its own digits each raised to the power
of the number of digits.
For Example : 153
No of Digit : 3
= (1*1*1) + (5*5*5) + (3*3*3)
= 1 + 125 + 27
= 153 Its an Armstrong Number
For Example : 1634
No of Digit : 4
= (1*1*1*1) + (6*6*6*6) + (3*3*3*3)+(4*4*4*4)
= 1 + 1296 + 81 + 256
= 1634 Its an Armstrong Number
// Source Code
// Note : This (Source Code)is For Three Digit Number (Integer)
//import java.util.*;
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num,rd,sum=0,ncopy;
System.out.print("Enter a Three Digit Number : ");
num = in.nextInt();
ncopy=num;
while(num!=0)
{
rd=num%10;
sum=sum+rd*rd*rd;
//sum=sum+(int)Math.pow(rd,3);
num=num/10;
}
if(sum==ncopy)
{
System.out.print("It is an Armstrong Number . ");
}
else
{
System.out.print("It is Not an Armstrong Number . ");
}
}
}
// Sample Input :
Enter a Three Digit Number : 153
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 370
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 371
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 123
// Sample Output :
It is Not an Armstrong Number
// Source Code
// Note : This (Source Code)is For N Number of Digit Number (Integer)
//import java.util.*;
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num,count=0,rd,sum=0,ncopy;
System.out.print("Enter a Number : ");
num = in.nextInt();
ncopy=num;
while(num!=0)
{
num=num/10;
count++;
}
num=ncopy;
while(num!=0)
{
rd=num%10;
sum=sum+(int)Math.pow(rd,count);
num=num/10;
}
if(sum==ncopy)
{
System.out.print("It is an Armstrong Number . ");
}
else
{
System.out.print("It is Not an Armstrong Number . ");
}
}
}
// Sample Input :
Enter a Number : 153
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Number : 5
// Sample Output :
It is an Armstrong Number
----------------------
Armstrong number is a number that is the sum of its own digits each raised to the power
of the number of digits.
For Example : 153
No of Digit : 3
= (1*1*1) + (5*5*5) + (3*3*3)
= 1 + 125 + 27
= 153 Its an Armstrong Number
For Example : 1634
No of Digit : 4
= (1*1*1*1) + (6*6*6*6) + (3*3*3*3)+(4*4*4*4)
= 1 + 1296 + 81 + 256
= 1634 Its an Armstrong Number
// Source Code
// Note : This (Source Code)is For Three Digit Number (Integer)
//import java.util.*;
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num,rd,sum=0,ncopy;
System.out.print("Enter a Three Digit Number : ");
num = in.nextInt();
ncopy=num;
while(num!=0)
{
rd=num%10;
sum=sum+rd*rd*rd;
//sum=sum+(int)Math.pow(rd,3);
num=num/10;
}
if(sum==ncopy)
{
System.out.print("It is an Armstrong Number . ");
}
else
{
System.out.print("It is Not an Armstrong Number . ");
}
}
}
// Sample Input :
Enter a Three Digit Number : 153
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 370
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 371
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Three Digit Number : 123
// Sample Output :
It is Not an Armstrong Number
// Source Code
// Note : This (Source Code)is For N Number of Digit Number (Integer)
//import java.util.*;
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num,count=0,rd,sum=0,ncopy;
System.out.print("Enter a Number : ");
num = in.nextInt();
ncopy=num;
while(num!=0)
{
num=num/10;
count++;
}
num=ncopy;
while(num!=0)
{
rd=num%10;
sum=sum+(int)Math.pow(rd,count);
num=num/10;
}
if(sum==ncopy)
{
System.out.print("It is an Armstrong Number . ");
}
else
{
System.out.print("It is Not an Armstrong Number . ");
}
}
}
// Sample Input :
Enter a Number : 153
// Sample Output :
It is an Armstrong Number
// Sample Input :
Enter a Number : 5
// Sample Output :
It is an Armstrong Number
0 Comments