JAVA 1.4 To check whether the number is an Armstrong number or not.

To check whether the number is an Armstrong number or not.

mport java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int result=0;
int t=n;
int x=n,r,c=0,sum=0;
while(t>0)
{
  c++;
  t=t/10;
}
while(x>0)
{
  r=x%10;
  sum=sum+(int)Math.pow(r,c);
  x=x/10;
}
if(sum==n)
  result=1;
else
  result=0;
System.out.print(result);

 }
}


Sample Input and output:
153
1

No comments:

Post a Comment