Write Java program to check if a number is Armstrong number or not?
An Armstrong number of 3 digit is a number for which sum of cube of its digits are equal to number e.g. 371 is an Armstrong number because 3*3*3 + 7*7*7 + 1*1*1 = 371).
Output:Enter the number :321
The given number is not armstrong number
import java.util.Scanner;
public class ArmstrongNumber{
public static void checkArmstrongNumber(int value){
int armstrongNumber=getArmstrongNumber(value);
if(value==armstrongNumber){
System.out.println("The given number is armstrong number");
}else{
System.out.println("The given number is not armstrong number");
}
}
public static int getArmstrongNumber(int number){
int remainder=0,reverse=0;
do{
remainder=number%10;
number=number/10;
reverse+=remainder*remainder*remainder;
}while(number>10);
int armstrongValue=reverse+(number*number*number);
return armstrongValue;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int number=sc.nextInt();
checkArmstrongNumber(number);
sc.close();
}
}
Output:Enter the number :321
The given number is not armstrong number
Enter the number: 371
The given number is armstrong number
Comments
Post a Comment