Writing a program to check if a number is an "Armstrong Number" (or a "narcissistic number") is a classic programming challenge. It's a great exercise to learn how to manipulate individual digits of a number using loops and math functions.
What is an Armstrong Number?
An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
Example (153):
- The number is 153.
- The number of digits is 3.
- The calculation is:
(1^3) + (5^3) + (3^3) 1 + 125 + 27 = 153- Since the result (
153) equals the original number (153), it is an Armstrong number.
Example (371):
- The number is 371.
- The number of digits is 3.
- The calculation is:
(3^3) + (7^3) + (1^3) 27 + 343 + 1 = 371- Since the result (
371) equals the original number (371), it is an Armstrong number.
What You Need to Know
To write this program, you will use a few basic PL/SQL concepts:
- Enabling Output: You must run this command once in your SQL tool:
SET SERVEROUTPUT ON; - Anonymous Block: We will write our code in a
DECLARE...BEGIN...END;block. - Variables: We'll need several:
v_num: The number we are testing.v_original_num: A copy of the original number for the final comparison.v_num_digits: A variable to store the count of digits.v_sum: A variable to hold the sum of the powers, initialized to0.v_remainder: A temporary variable to hold the last digit.
LENGTH(TO_CHAR(n)): A simple way to count the number of digits inn.WHILELoop: To extract each digit, one by one.MOD(n, 10): To get the last digit of a number.TRUNC(n / 10): To remove the last digit from a number.POWER(n, p): To raise a numbernto a powerp.
PL/SQL Program: Check for Armstrong Number
This program will check the number stored in v_num and print whether it is an Armstrong number or not.
PL/SQL Program
SET SERVEROUTPUT ON;
DECLARE
-- The number we want to test
v_num NUMBER := 153;
-- A variable to hold the sum of the powers
v_sum NUMBER := 0;
-- A temporary variable to hold the last digit
v_remainder NUMBER;
-- A variable to hold a copy of the original number
v_original_num NUMBER;
-- A variable to hold the number of digits
v_num_digits NUMBER;
BEGIN
-- 1. Store a copy of the original number
v_original_num := v_num;
-- 2. Count the number of digits
v_num_digits := LENGTH(TO_CHAR(v_num));
-- 3. Start the loop to extract digits and calculate the sum
WHILE v_num > 0 LOOP
-- Get the last digit (e.g., 153 -> 3)
v_remainder := MOD(v_num, 10);
-- Add (digit ^ num_digits) to the sum
-- e.g., v_sum = 0 + POWER(3, 3) = 27
v_sum := v_sum + POWER(v_remainder, v_num_digits);
-- Remove the last digit from the number (e.g., 153 -> 15)
v_num := TRUNC(v_num / 10);
END LOOP;
-- 4. Compare the original number to the final sum
IF v_original_num = v_sum THEN
DBMS_OUTPUT.PUT_LINE(v_original_num || ' is an Armstrong number.');
ELSE
DBMS_OUTPUT.PUT_LINE(v_original_num || ' is NOT an Armstrong number.');
END IF;
END;
/
Result (for n := 153)
153 is an Armstrong number.
Result (if you change to n := 1634)
1634 is an Armstrong number.
Result (if you change to n := 125)
125 is NOT an Armstrong number.
Program Explanation
DECLAREsection: We create our variables.v_numis set to153.v_sumis set to0.BEGINsection: The logic starts.v_original_num := v_num;: We store a copy of153. We need this because the loop will destroy thev_numvariable (it will end up as0).v_num_digits := LENGTH(TO_CHAR(v_num));: This converts153to the string'153'and gets itsLENGTH, which is3. We store3inv_num_digits.WHILE v_num > 0 LOOP: The loop begins and will run as long asv_numis positive.
Inside the loop:
- Loop 1:
v_num= 153v_remainder=MOD(153, 10)= 3v_sum=0 + POWER(3, 3)= 27v_num=TRUNC(153 / 10)= 15 (Loop continues)
- Loop 2:
v_num= 15v_remainder=MOD(15, 10)= 5v_sum=27 + POWER(5, 3)=27 + 125= 152v_num=TRUNC(15 / 10)= 1 (Loop continues)
- Loop 3:
v_num= 1v_remainder=MOD(1, 10)= 1v_sum=152 + POWER(1, 3)=152 + 1= 153v_num=TRUNC(1 / 10)= 0 (Loop stops)
<!-- end list -->
IF v_original_num = v_sum THEN: The program comparesv_original_num(153) to the finalv_sum(153).- Since
153 = 153isTRUE, it prints "153 is an Armstrong number."
