This is a Java program that checks whether a given three-digit number is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits.
The program prompts the user to input a three-digit number and then performs the following steps:
Note that this program assumes that the user enters a three-digit number, and will not work correctly if the user enters a number with a different number of digits.
import java.util.Scanner; public class ArmstrongNumber { //Write a program to check whether the given 3 digit number is armstrong or not public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter 3 Digit Number : "); int number = in.nextInt();//123 int temp = number;//123 int digit1, digit2, digit3; digit3=temp%10;//3 temp=temp/10;//12 digit2=temp%10;//2 temp=temp/10;//1 digit1=temp%10;//1 int result=(digit1 * digit1 * digit1)+( digit2 * digit2 * digit2)+(digit3 * digit3 * digit3); if(number==result){ System.out.println(number + " is armstrong Number"); }else{ System.out.println(number + " is not an armstrong Number"); } } }
Enter 3 Digit Number : 371 371 is armstrong Number
Enter 3 Digit Number : 634 634 is not an armstrong NumberTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions