Armstrong number using java









An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1*1*1+ 5*5*5 + 3*3*3=153 .
Example:

import java.util.Scanner;

public class Amstrong 
    {


 public static void main(String[] args) 
           {


int temp,ans,sum=0;
System.out.println(" Enter any number:");
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();

temp=n;
while(temp!=0)
{
ans=temp%10;
sum=sum+ans*ans*ans;
temp=temp/10;
}

if(n==sum)
{
System.out.println(" Is an Amstrong Number");
}
else
{
System.out.println(" Not an Amstrong Number");
}
}


}

Output:

 Enter any number:
153
 Is an Amstrong Number



No comments:

Post a Comment