An Armstrong number (also known as a narcissistic number or a pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits.
For example, the number 153 is an Armstrong number because:
1^3 + 5^3 + 3^3 = 153
Similarly, the number 371 is an Armstrong number because:
3^3 + 7^3 + 1^3 = 371
And the number 9474 is an Armstrong number because:
9^4 + 4^4 + 7^4 + 4^4 = 9474
So, in general, if a number n
has d
digits, it is an Armstrong number if the sum of each digit raised to the power d
is equal to n
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import tkinter as tk from tkinter import messagebox def check_armstrong(): number = int(entry.get()) sum = 0 temp = number length = len(str(number)) while temp > 0: digit = temp % 10 sum += digit ** length temp //= 10 if number == sum: messagebox.showinfo("Result", str(number) + " is an Armstrong number") else: messagebox.showinfo("Result", str(number) + " is not an Armstrong number") root = tk.Tk() root.title("Armstrong Number Checker") label = tk.Label(root, text="Enter a number:") entry = tk.Entry(root) button = tk.Button(root, text="Check", command=check_armstrong) label.pack() entry.pack() button.pack() root.mainloop() |
In this program, a GUI is created using the Tkinter library to allow the user to enter a number and check if it is an Armstrong number. The check_armstrong
function performs the calculation to determine if the number is an Armstrong number and displays the result using a messagebox
from Tkinter.
Here is an explanation of the code, line by line:
1 2 3 4 | import tkinter as tk from tkinter import messagebox |
This imports the Tkinter library and the messagebox
module, which is used to display messages in a pop-up window.
1 2 3 4 | def check_armstrong(): number = int(entry.get()) |
This defines the check_armstrong
function, which is called when the user clicks the “Check” button. The function starts by converting the string entered in the text entry field to an integer and storing it in the number
variable.
1 2 3 4 5 | sum = 0 temp = number length = len(str(number)) |
The sum
variable is used to store the sum of the digits raised to the power of the number of digits in the input number. temp
is a variable that is used to store the original number for processing. length
stores the number of digits in the input number.
1 2 3 4 5 6 | while temp > 0: digit = temp % 10 sum += digit ** length temp //= 10 |
This is the loop that performs the calculation to determine if the number is an Armstrong number. The loop continues as long as temp
is greater than 0. For each iteration of the loop, the last digit of temp
is extracted by taking the remainder of temp
divided by 10, and stored in the digit
variable. Then, digit
is raised to the power of length
and added to the sum
. Finally, temp
is updated by integer division by 10 to remove the last digit.
1 2 3 4 5 6 | if number == sum: messagebox.showinfo("Result", str(number) + " is an Armstrong number") else: messagebox.showinfo("Result", str(number) + " is not an Armstrong number") |
After the loop has completed, the function checks if the number
is equal to sum
. If they are equal, the number is an Armstrong number, and a message box is displayed to indicate this. If they are not equal, the number is not an Armstrong number, and a message box is displayed to indicate this.
1 2 3 4 | root = tk.Tk() root.title("Armstrong Number Checker") |
This creates a Tkinter window object and sets its title to “Armstrong Number Checker”.
1 2 3 4 5 | label = tk.Label(root, text="Enter a number:") entry = tk.Entry(root) button = tk.Button(root, text="Check", command=check_armstrong) |
These three lines create the label, text entry, and button widgets that make up the user interface. The label
displays the text “Enter a number:”, the entry
allows the user to enter a number, and the button
allows the user to check if the entered number is an Armstrong number.
1 2 3 4 5 | label.pack() entry.pack() button.pack() |
These lines arrange the widgets in the window using the pack
geometry manager.
1 2 3 | root.mainloop() |
This starts the Tkinter event loop, which displays the window and listens for user events (such as clicking the button). The program will continue to run until the window is closed.