Monday, 5 January 2015

Java Program to check whether a number is an Armstrong number or not


This a Java program to check whether a given number is armstrong or not .
Generally a number is said to be an armstrong number if the cubes of the digits is equal to its original number .

For Example : 153 is an armstrong number as 1³ + 5³ + 3³ = 153  whereas 234 is not an armstrong number as 2³+ 3³+ 4³ is not equal to 234.


PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class Armstrong {
 public static void main(String[] args) {
  int number, num, sum = 0, remainder;
  Scanner scan = new Scanner(System.in);
  System.out.println("Enter any number :\t");
  number = scan.nextInt();
  scan.close();
  num = number;
  while (num != 0) {
   remainder = num % 10;
   sum = sum + remainder * remainder * remainder;
   num = num / 10;
  }
  if (number == sum)
   System.out.println(number + "  is an Armstrong number");
  else
   System.out.println(number + "  is an not an Armstrong number");
 }
}

OUTPUT :






Related Programs :
Palindrome number in C      
Palindrome number in Cpp
Reverse of a number in Java      
Palindrome number in Java
Reverse of a number in C       
Reverse Of a number in Cpp
Armstrong number in C 
Armstrong number in Cpp

No comments:

Post a Comment