Friday 27 July 2012

Armstrong Number

What is Armstrong Number ?
   An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.

for example:
  Lets consider the following number:
                                                         153
                                                         370
                                                         371
                                                         407

153=1^3+5^3+3^3
      =153

370=3^3+7^3+0^3
      =370

371=3^3+7^3+1^3
      =371

407=4^3+0^3+7^3
      =407

//Program to check Whether given number is armstrong number or not by using Java.
class ArmstrongNumber
{
   public static void main(String args[])
   {
     int a,b,c=0,n=153;
     a=n;
     while(n>0)
     {
        b=n%10;
        c=c+(b*b*b);
        n=n/10;
     }
    if(a==c)
    {
      System.out.println("This number is Armstrong number:"+c);
     }
    else
     {
       System.out.println("This number is not Armstrong number:"+c);
     }
  }
}


Save this program as      ArmstrongNumber.java

Compile it
                                     javac ArmstrongNumber.java

Run it 
                                     java ArmstrongNumber

Output
                                     
                                    This number is Armstrong number:153

If you have any trouble or wants some more program then just reach @ nihalcse@gmail.com

ShareThis

Related Posts Plugin for WordPress, Blogger...