This program will check a number if it is Armstrong number. For example: 153 is an Armstrong number where 13+53+33 = 1+125+27 = 153. Thus 407 = 43 + 03 + 73 = 64 + 0 + 343 = 407 is also an Armstrong number.
import java.util.Scanner;
public class ArmstrongNumber{
public static void main(String args []){
//declaring integer type variables
int num, temp, r=0, sum=0;
//getting input from keyboard
System.out.print("Enter a number: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
//keeping the number into temp variable
temp = num;
//while loop to check whether the number is armstrong or not
while(temp != 0){
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
//if...else condition to print the result
if(num==sum){
System.out.println(num+" is an Armstrong");
} else{
System.out.println(num+" is not an Armstrong");
}
}
}

No comments:
Post a Comment