Armstrong or Not

Objective

To implement a program that checks whether a given three-digit number qualifies as an Armstrong number or not.

 

Theory

An Armstrong number is a special kind of number in math. An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits. 

 

 For example, let's take two 3-digit numbers 153 and 125:   

                          13 + 53 + 33 = 1 + 125 + 27 = 153 

                          13 + 23 + 53    = 1 + 8 + 125 = 134 

So, 153 is an Armstrong number. Whereas 125 is not an Armstrong number. 

Let's say we have an n-digit number abcd... (where each letter represents a digit). 

Then 

   an +bn +cn + dn.+….= The number itself 

 

Instructions

The program to check whether the given number is Armstrong or not and its Low-level language instructions used are as following: 

add It adds the values contained in the register and store the result in first register.  
assign It assigns a value to a variable.
div It divides the values contained in two registers and puts the result in the first register. 
eq It is the equal to operator,  check if the values in two registers are same. 
gt Greater than operator, checks whether the first operand is greater than second operand and return a boolean value based on the condition. 
if _false  Checks if the condition evaluated to be false. 
if_true Checks if the condition evaluated to true 
in It reads the input from the user and stores it in a variable. 
label Is used to mark the position in the low-level instructions to enable jumping to the marked position. 
load It loads a value of a global or local variable to the given register. 
mul It multiplies the values contained in two registers and puts the result in the first register. 
out It prints the output value to the screen. 
rem It performs division operation of the values contained in two registers and puts the remainder in the first register. 
store It stores the value from the given register to the global or local variable. 
sub It subtracts the value contained in the second register from the first register. 

 

Variables used in the Python program: 

  • n - Variable to store the user input (3-digit number).
  • sum - Variable to store the sum of cubes of individual digits. 
  • t0 - Variable to store the current value of the input number. 
  • dig - Variable to store the last digit of the current number. 

 

Learning Outcomes

The provided Python program aims to determine whether a given number is an Armstrong number or not. Below are the few learning objectives associated with this program. 

  • Understand how to receive user input in Python using the input() function and learn how to store input in variables and manipulate them within the program. 
  • To understand the significance of using temporary variables in certain algorithms. 
  • Comprehend the use of a while loop for iterative processing and understand the loop condition and how it controls the execution of the block of code. 
  • Learn how to perform basic arithmetic operations (%, //) in Python and understand how these operations are used in extracting digits and updating the temporary variable. 
  • Understand the algorithm for checking if a given number is an Armstrong number.