Armstrong number in Programming is a popular logical problem which is attempted by anyone who is into quantitative reasoning or learning programming language. It is a unique problem where the armstrong number is calculated by adding each digit of a number to the power of the number of digits. Are you familiar with the Armstrong number and logic behind the working of this famous mathematical problem?
In this article, we are going to learn how to create a program to print Armstrong numbers using C, Python, Java, Javascript, and other programming languages.
What is the Armstrong Number In Programming?
An Armstrong Number in Programming is a number which is formed by adding up all digits of a number raised to the power of the total digits in that number. It is also known as narcissistic numbers. It was named after a mathematician whose name was Michale F. Armstrong in 1969. Let us observe the syntax of calculating an Armstrong number in programming language.
![]() |
For example, 372 is a number and we can calculate the armstrong number by following the method below.
372 = 3^3 + 7^3 + 2^3 = 27 + 343+ 8 = 378 |
In “372” there are 3 digits in the number and we have to sum each digit raised to the power of the total number of digits in the number. This is how you can calculate the armstrong number in programming language for any given number.
Algorithm to Solve Armstrong Numbers
Let us analyse a simple step by step process to solve an armstrong number using programming languages.
- Firstly, we have to count the number of digits in the number and it is stored in a variable.
- Now the sum of digits of a number can be performed using the num mod 10 in python where mod is a reminder operation. In C programming we use divide and modulus operation.
- The individual digit is then stored in a variable.
- The number gets divided by 10 to remove the last number and obtain the next digit.
- All these steps are repeated until the number value is greater than 0 itself.
- When the number gets below 0 then we end the while loop.
- Now we have to use an if else statement to check whether the number matches the sum of the digits.
- If Yes then return with true otherwise return with false value.
Examples of Armstrong Numbers
Let us analyse and walk through a few examples to get familiar with Armstrong numbers.
Number | Calculation |
153 | 13+53+33=1+125+27=15313+53+33=1+125+27=153 |
370 | 33+73+03=27+343+0=37033+73+03=27+343+0=370 |
371 | 33+73+13=27+343+1=37133+73+13=27+343+1=371 |
407 | 43+03+73=64+0+343=40743+03+73=64+0+343=407 |
1634 | 14+64+34+44=1+1296+81+256=1634 |
9474 | 94+44+74+44=6561+256+2401+256=9474 |
Now, we can see that all these numbers on evaluating matches the original number hence they are armstrong numbers. If they did not match the original number on calculation then they are not armstrong numbers.
Armstrong Number In C Programming
We can simply use a while loop and modulus operator to calculate the armstrong number using C language. Let us understand how to calculate Armstrong numbers using the C programming language.
#include <stdio.h>
#include <math.h> int isArmstrong(int num) { int sum = 0, temp = num, digits = 0; while (temp != 0) { // Count digits digits++; temp /= 10; } temp = num; while (temp != 0) { int digit = temp % 10; sum += pow(digit, digits); temp /= 10; } return (sum == num); } int main() { int num; printf(“Enter a number: “); scanf(“%d”, &num); if (isArmstrong(num)) printf(“%d is an Armstrong number.\n”, num); else printf(“%d is not an Armstrong number.\n”, num); return 0; } |
- The function isArmstrong (int num) accepts an integer as input and checks if it is an armstrong number.
- It starts with the “sum = 0” variable where it stores the sum of the digits raised to the power.
- “temp = num” is used to process the number without changing the original number.
- “digits = 0” is used to count the number of digits.
- You will extract the last digit using the modulus operator which is raised to the power of total digits in the number.
- Remove the last digit in the next iteration by using the “temp /= 10.
- If the value of “sum = num”, return true otherwise return false.
Armstrong Number In Python Programming Language
Let us know how to calculate Armstrong number using Python programming language.
def is_armstrong(num):
power = len(str(num)) return num == sum(int(digit) ** power for digit in str(num)) num = int(input(“Enter a number: “)) if is_armstrong(num): print(f”{num} is an Armstrong number.”) else: print(f”{num} is not an Armstrong number.”) |
In the given function the logic used is similar to the above only the syntax is different. We are using a list comprehension to calculate the sum and power of the total digit and then add them up to all the values to get the armstrong number.
Armstrong Number In Java Programming Language
import java.util.Scanner;
public class ArmstrongNumber { public static boolean isArmstrong(int num) { int sum = 0, temp = num; int digits = String.valueOf(num).length(); while (temp != 0) { int digit = temp % 10; sum += Math.pow(digit, digits); temp /= 10; // Remove last digit } return sum == num; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int num = scanner.nextInt(); scanner.close(); if (isArmstrong(num)) System.out.println(num + ” is an Armstrong number.”); else System.out.println(num + ” is not an Armstrong number.”); } } |
In Java programming , we are using a similar approach to find the armstrong number and decide whether the given number is an armstrong number or not. We are using a built in method i,e. Math.pow() to calculate the power of a given number.
Now, from above we conclude that C uses manual digit extraction that we can also follow in ahead, while Python uses list comprehension and Java uses loops and Math.pow ( ). Let us know some applications of armstrong number in programming language below
Application of Armstrong Number in Programming
Some of the many benefits of using Armstrong numbers are given below.
- Armstrong numbers are used by beginners to understand the programming concepts and use them to learn and practice coding.
- In certain types of cryptographic protocols Armstrong numbers are used as a checksum validator.
- The concepts of armstrong is used in digital manipulation and getting familiar with concepts like powers, digit based calculations, patterns and more.
- They are also used in feature extraction and pattern recognition models for numerical data.
- It is also used in puzzle and game development by using the concepts of Armstrong number.
Armstrong Number in Programming Language FAQs
Q1. What is an armstrong number?
Ans: An Armstrong Number is a number which is formed by adding up all digits of a number raised to the power of the total digits in that number. It is also known as narcissistic numbers.
Q2. Can Armstrong number be solved using programming?
Ans: Yes, the Armstrong number is a mathematical problem which can easily be solved using programming languages like Python, C++, Java or Javascript.
Q3. What is an application of Armstrong Number?
Ans: Armstrong numbers are used in mathematical puzzles and for education and training purposes in programming.
Q4. Is 372 an armstrong number?
Ans: No, 372 is not a Armstrong number because when we calculate
372 = 3^3 + 7^3 + 2^3 = 27 + 343+ 8 = 378 then it does not satisfy the armstrong criteria.