Write a program in JAVA to input a number and check if it is armstrong or not
Armstrong Number : Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers
import java.io.*;
public class Armstrong {
public static void main(String[] args)throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Positive Number : ");
int num=Integer.parseInt(obj.readLine());
int ldigit,cube=0;
int temp=num;
while (temp!=0) {
ldigit=temp%10;
cube+=ldigit*ldigit*ldigit;
temp=temp/10;
}
if (cube == num) {
System.out.println(num+" Is Armstrong Number");
} else {
System.out.println(num+" Is Not an Armstrong Number");
}
}
}
OUTPUT
1.
Enter A Number : 371
371 is armstrong number
2.
Enter A Number : 476
476 is not a armstrong number
0 Comments:
Post a Comment