Posted by: Sourav | December 12, 2013

Armstrong Number in Java


import java.util.*;
 
class ArmstrongNumber
{
   public static void main(String args[])
   {
      int n, sum = 0, temp, r;
 
      Scanner in = new Scanner(System.in);
      System.out.println(“Enter a number”);     
      n = in.nextInt();
 
      temp = n;
 
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
 
      if ( n == sum )
         System.out.println(“armstrong number.”);
      else
         System.out.println(“not an armstrong number.”);        
   }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: