Q: Write a Java program to check if a given number is an Armstrong number. An Armstrong number (also known as a narcissistic number) for a given number of digits is one where the sum of its digits each raised to the power of the number of digits equals the number itself. For example, for a 3-digit number, the sum of the cubes of its digits should equal the number.

Armstrong-number-in-java

Employee Hierarchy:

An Armstrong number is a special kind of number in mathematics. It’s also known as a narcissistic number or a plenary number. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number.

Certainly! Here’s how Armstrong numbers work:

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. Let’s break down how to identify an Armstrong number using an example:

Consider the number 153:

1. Count the number of digits in 153. In this case, there are 3 digits.

2. Raise each digit to the power of the total number of digits (which is 3 in this case), and sum them up.

EXAMPLE

For 153:
1^3 = 1
5^3 = 125
3^3 = 27
Sum of 1 + 125 + 27 = 153
2. Second example

1: 11 = 1

2: 21 = 2

3: 31 = 3

153: 13 + 53 + 33 = 1 + 125+ 27 = 153

125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)

1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643

 

3. If the sum obtained is equal to the original number (153 in this case), then the number is an Armstrong number.

So, 153 is an Armstrong number because it equals the sum of its digits raised to the power of the number of digits.

In the provided Java code, the program takes an input number, checks if it’s an Armstrong number using the algorithm described above, and outputs the result. You can input various numbers to see whether they are Armstrong numbers or not.

 
				
					import java.util.Scanner;  
import java.lang.Math;  
public class ArmstsrongNumberExample  
{  
//function to check if the number is Armstrong or not  
static boolean isArmstrong(int n)   
{   
int temp, digits=0, last=0, sum=0;   
//assigning n into a temp variable  
temp=n;   
//loop execute until the condition becomes false  
while(temp>0)    
{   
temp = temp/10;   
digits++;   
}   
temp = n;   
while(temp>0)   
{   
//determines the last digit from the number      
last = temp % 10;   
//calculates the power of a number up to digit times and add the resultant to the sum variable  
sum +=  (Math.pow(last, digits));   
//removes the last digit   
temp = temp/10;   
}  
//compares the sum with n  
if(n==sum)   
//returns if sum and n are equal  
return true;      
//returns false if sum and n are not equal  
else return false;   
}   
//driver code  
public static void main(String args[])     
{     
int num;   
Scanner sc= new Scanner(System.in);  
System.out.print("Enter the limit: ");  
//reads the limit from the user  
num=sc.nextInt();  
System.out.println("Armstrong Number up to "+ num + " are: ");  
for(int i=0; i<=num; i++)  
//function calling  
if(isArmstrong(i))  
//prints the armstrong numbers  
System.out.print(i+ ", ");  
}   
}  
				
			
				
					Enter a number: 153
153 is an Armstrong number.

				
			
				
					Enter a number: 123
123 is not an Armstrong number.

				
			

Another example

				
					import java.util.Scanner;

public class ArmstrongNumber {
    
    // Function to count the number of digits
    static int countDigits(int number) {
        int count = 0;
        while (number != 0) {
            number /= 10;
            count++;
        }
        return count;
    }
    
    // Function to check if a number is an Armstrong number
    static boolean isArmstrong(int number) {
        int originalNumber = number;
        int numDigits = countDigits(number);
        int sum = 0;
        
        while (number != 0) {
            int digit = number % 10;
            sum += Math.pow(digit, numDigits);
            number /= 10;
        }
        
        return sum == originalNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        
        if (isArmstrong(number)) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
        
        scanner.close();
    }
}

				
			
				
					Enter a number: 123
123 is not an Armstrong number.

				
			
				
					Enter a number: 153
153 is an Armstrong number.

				
			
prime number

1. The program begins by defining a function `countDigits` that takes an integer as input and returns the count of digits in that number. This function is used to calculate the number of digits in the input number.

2. Another function named `isArmstrong` is defined. This function takes an integer as input and checks whether it’s an Armstrong number or not.

3. Inside the `isArmstrong` function, the original input number is stored because it’s needed for comparison later. Then, the number of digits in the input number is calculated using the `countDigits` function.

4. A variable `sum` is initialized to store the sum of the digits raised to the power of the number of digits.

5. A while loop is used to iterate through each digit of the input number. Inside the loop, the last digit of the number is extracted using modulo (`% 10`) and stored in a variable called `digit`. Then, this digit is raised to the power of the number of digits and added to the `sum`. Finally, the last digit is removed from the number by dividing it by 10.

6. After the loop, the `sum` is compared with the original input number. If they are equal, it means the input number is an Armstrong number, and the function returns `true`. Otherwise, it returns `false`.

7. In the `main` method, the program prompts the user to enter a number.

8. The input number is then passed to the `isArmstrong` function to check if it’s an Armstrong number or not.

9. Depending on the result, the program prints out whether the input number is an Armstrong number or not.

This program effectively demonstrates the concept of Armstrong numbers in Java.

Scroll to Top