Java program to find Armstrong number


This a a program to find  a Armstrong number in Java programming language

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.

Armstrong number
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 to check if it is an armstrong 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("Entered number is an armstrong number.");
      else
         System.out.println("Entered number is not an armstrong number.");         

   }
}
Questions based on the program are to be asked in the comments section below

Comments

Popular posts from this blog

Practice questions for English grammar

Physics 2017 Solved Paper Physics

Computer applications icse board exam paper solved 2017