Java Program to Display Armstrong Number using Function
What is an Armstrong ?
An Armstrong number, also known as a narcissistic number, is a number that is the sum of its own digits when each digit is raised to the power of the number of digits in the number. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371
Armstrong numbers are not only intriguing but also relatively rare. They serve as a captivating subject for exploration in mathematics and programming.
Algorithm for Armstrong Number in Java
- Define a function isArmstrongNumber() that takes an integer num as input and returns a boolean value.
- Declare a variable originalNum and set it equal to num.
- Convert num to a string and find the length of the string. This will give us the number of digits in num.
- Declare a variable sum and set it to 0. This will be used to store the sum of the digits raised to the power of the number of digits.
- Begin a loop that will iterate as long as num is greater than 0.
- Inside the loop, find the last digit of num by using the modulus operator (%) to get the remainder when num is divided by 10.
- Raise the digit to the power of the number of digits, and add the result to sum.
- Divide num by 10 to remove the last digit.
- End the loop.
- After the loop, check if originalNum is equal to sum. If it is, return true. Otherwise, return false.
Here is a pseudo-code algorithm for checking whether a given number is an Armstrong number:
FUNCTION isArmstrongNumber(num: INTEGER) -> BOOLEAN: originalNum = num numOfDigits = LENGTH(num AS STRING) sum = 0 WHILE num > 0: digit = num MOD 10 sum = sum + digit^numOfDigits num = num DIV 10 END WHILE RETURN originalNum == sum END FUNCTION
Here are a few examples of Armstrong numbers:
- 153 (1^3 + 5^3 + 3^3 = 153)
- 371 (3^3 + 7^3 + 1^3 = 371)
- 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)
Example :
public class Main { public static void main(String[] args) { int num = 153; if (isArmstrongNumber(num)) { System.out.println(num + " is an Armstrong number."); } else { System.out.println(num + " is not an Armstrong number."); } } public static boolean isArmstrongNumber(int num) { int originalNum = num; int numOfDigits = String.valueOf(num).length(); int sum = 0; while (num > 0) { int digit = num % 10; sum = sum + (int) Math.pow(digit, numOfDigits); num = num / 10; } return originalNum == sum; } }
Output :
153 is an Armstrong number.
Example : Display Armstrong Number Using isArmstrong() Function
public class Main { public static void main(String[] args) { System.out.println("Armstrong numbers between 1 and 1000:"); for (int number = 1; number <= 1000; number++) { if (isArmstrong(number)) { System.out.println(number); } } } public static boolean isArmstrong(int number) { int originalNumber = number; int sum = 0; int digits = String.valueOf(number).length(); while (number != 0) { int digit = number % 10; sum += Math.pow(digit, digits); number /= 10; } return originalNumber == sum; } }
Output :
Armstrong numbers between 1 and 1000: 1 2 3 4 5 6 7 8 9 153 370 371 407
Explanation :
The program prompts the user to enter the lower and upper bounds of the interval, and then uses a for loop to iterate through all the numbers in that interval. For each number, the program calls the isArmstrong() function to check if the number is an Armstrong number. If it is, the program prints the number.
The isArmstrong() function takes an integer as an input and it first initialize a variable originalNum which is equal to the input number and sum = 0. Then it uses while loop to extract each digit of the number and cube it and add the result to the sum. Then it divides the number by 10 to get next digit. Finally, it compares the original number with the sum, if it’s equal, it returns true otherwise false.
For example, if the user enters lower bound as 1 and upper bound as 1000, the program will display all the Armstrong numbers between 1 and 1000, which are 153, 370, 371, and 407.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment