Tuesday, June 30, 2015

Check Armstrong no . in java

  1. 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, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.


import java.util.*;

public class Armstrong
{

public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("enter a no. to check no is armstrong or not");

int n=scn.nextInt();
int c=0,a,b;
b=n;
while(n>0)
{
 a=n%10;
 n=n/10;
 c=c+(a*a*a);
}
if(b==c)
{
  System.out.println("number is armstrong" );
}

else
{
  System.out.println("no. is not armstrong" );
}

}
}

Output :

enter a no. to check no is armstrong or not
371
number is armstrong


0 comments:

Post a Comment