For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
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.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.