Tutorial Playlist
Today, we'll be exploring a unique category of numbers known as Armstrong numbers. But what exactly is it? It's a number that equals the sum of its own digits, each raised to the power of the number of digits.
Now, you might be wondering how to find Armstrong numbers. That's where the "Armstrong number program" comes in handy. With programming languages like Java and C++, you can create a program to identify these intriguing numbers. Whether you're a Math enthusiast or a programming beginner, this concept offers an exciting opportunity to blend number theory with coding.
In this guide, we'll touch upon different aspects of Armstrong numbers. We'll start with the "Armstrong number in Java", looking at how this powerful language helps identify these unique numbers. Next, we'll dive into Python to see how "Armstrong Number Python" programs work.
Of course, we can't leave out C and C++, two foundational languages in the programming world. We'll explore the specifics of creating an "Armstrong number in C" program and see how "Armstrong number C++" programs work. We'll also discuss how to perform an "Armstrong number check" to verify these digits.
In the C programming language, an Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 153 is an Armstrong number because 1^3+ 5^3 + 3^3 = 153.
Here's a simple C program to check if a number is an Armstrong number:
Here, you enter a number. The program then splits it into its individual digits, cubes them, and adds them together. If the resulting sum equals the original number, the program will tell you that it's an Armstrong number. If not, it will say it's not an Armstrong number.
Output:
When you run the above C program, it will ask for a number. Let's say you enter '153'. Here's what you would see in your command-line interface:
In this case, the program recognizes '153' as an Armstrong number because the sum of the cubes of its digits equals the original number (1^3 + 5^3 + 3^3 = 153).
If you enter a non-Armstrong number, let's say '123', the output would be:
Here, the sum of the cubes of its digits does not equal the original number (1^3 + 2^3 + 3^3 = 36), so '123' is not an Armstrong number.
Here is a simple algorithm for finding a 3-digit Armstrong number.
Flowchart for the Armstrong Number (3-Digit):
Example:
Take 371.
An example of a C program that checks whether a three-digit number is an Armstrong number or not.
Output:
And for the input number 123:
The Armstrong Number in Java for a three-digit number allows one to determine whether a given number is an Armstrong number. Possible uses of this program are:
Here's an example of a Java program that checks whether a three-digit number is an Armstrong number or not.
Output:
Here, the program calculated the sum of the cubes of each digit: 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. Since it is equal to the original number, the program determined that it is an Armstrong number and displayed the corresponding message.
Explanation:
Here's an example of a Python program that checks whether a three-digit number is an Armstrong number or not.
output for input number 371:
Here, the program calculated the sum of the cubes of each digit: 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. Since it is equal to the original number, the program determined that it is an Armstrong number and displayed the corresponding message.
Explanation:
Here's a program in C++ to check if a number is an Armstrong number:
When you run the C++ program, it will ask for a number. Let's say you enter '9474'. Here's what you would see on your console:
In this case, the program recognizes '9474' as an Armstrong number because the sum of the 4th power of its digits equals the original number (9^4 + 4^4 + 7^4 + 4^4 = 9474).
Output:
Output:
Output:
The C++ algorithm receives a number as input and calculates its length. Then, it splits the number into individual digits, raises each digit to the power of the number's digit count, and adds them together. If the resulting sum equals the original number, the program prints that it's an Armstrong number. Otherwise, it's not.
This algorithm has a time complexity of O(n), where n is the number of digits. This is because each digit is processed individually.
For example, with the number 9474, the algorithm calculates the length (4), then processes each digit: 9^4, 4^4, 7^4, 4^4. Since the sum equals the original number, it outputs that 9474 is an Armstrong number.
The Java algorithm is very similar to the C++ one. The difference lies in the specific syntax and conventions of Java, such as the use of the Math.pow function for exponentiation and Scanner for input.
Its time complexity is also O(n), for the same reasons as the C++ algorithm.
The Python algorithm is more concise due to Python's built-in functions and simple syntax. It uses the len() function to find the number of digits and ** for exponentiation.
The time complexity is also O(n), just like the other two algorithms.
In all three languages, the algorithms break the number down into individual digits and perform calculations on each. The only difference is due to each language's specific syntax and features.
It is a concept in Computer Science that deals with the amount of computational time taken by an algorithm to run as a function of the size of the input to the program. It's generally expressed using Big O notation, which describes the upper bound of the time complexity in the worst-case scenario.
Let's consider a simple linear search algorithm in Python.
The time complexity of this algorithm is O(n), where 'n' is the length of the array. This is because, in the worst-case scenario (when the element is not in the array), the algorithm would need to traverse the entire array.
If you run the linear search function with an array and a search element, you get the index of the element if it is in the array or -1 if it is not.
For example:
Output:
It is another concept in Computer Science that represents the amount of memory an algorithm needs to run to completion. It's often expressed using Big O notation.
Let's consider a simple algorithm that sums the elements in an array:
In this case, the space complexity is O(1), which means the memory required by the algorithm is constant, regardless of the size of the array. This is because the algorithm only uses one variable (sum) to hold the total, and this doesn't change regardless of the size of the input array.
When you run the sum array function with an array as input, you get the sum of the elements in the array. For example,
Output:
Understanding the concept of Armstrong numbers and how to implement their identification in various programming languages like C++, Java, and Python is an important skill for every programmer. These provide hands-on knowledge about loops, conditionals, and mathematical operations, all of which are fundamental to programming.
These numbers are largely theoretical and used for educational purposes to understand programming and number theory concepts. Even so, they come up in checksum algorithms where having a number's individual digits contain information about the whole is beneficial.
The term "Narcissistic number" comes from the Greek myth of Narcissus, who fell in love with his own reflection. Similarly, Armstrong numbers can be "in love with themselves," as they can be expressed as a combination of their own digits raised to the power of the number of digits.
An efficient method to generate all Armstrong numbers of n digits would be to use a nested loop where the outer loop goes from 1 to n. The inner loop generates all n-digit numbers and checks if they are Armstrong numbers.
A classic example is 9474 because 9^4 + 4^4 + 7^4 + 4^4 equals 9474. To identify a 4-digit Armstrong number, we could write a program that takes a four-digit number as input, raises each digit to the fourth power, adds those values together, and then checks if the resulting sum equals the original number.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...