PL/SQL Program for Armstrong Number

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):

  1. The number is 153.
  2. The number of digits is 3.
  3. The calculation is: (1^3) + (5^3) + (3^3)
  4. 1 + 125 + 27 = 153
  5. Since the result (153) equals the original number (153), it is an Armstrong number.

Example (371):

  1. The number is 371.
  2. The number of digits is 3.
  3. The calculation is: (3^3) + (7^3) + (1^3)
  4. 27 + 343 + 1 = 371
  5. 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:

  1. Enabling Output: You must run this command once in your SQL tool:SET SERVEROUTPUT ON;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. 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 to 0.
    • v_remainder: A temporary variable to hold the last digit.
  4. LENGTH(TO_CHAR(n)): A simple way to count the number of digits in n.
  5. WHILE Loop: To extract each digit, one by one.
  6. MOD(n, 10): To get the last digit of a number.
  7. TRUNC(n / 10): To remove the last digit from a number.
  8. POWER(n, p): To raise a number n to a power p.

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

  1. DECLARE section: We create our variables. v_num is set to 153. v_sum is set to 0.
  2. BEGIN section: The logic starts.
  3. v_original_num := v_num;: We store a copy of 153. We need this because the loop will destroy the v_num variable (it will end up as 0).
  4. v_num_digits := LENGTH(TO_CHAR(v_num));: This converts 153 to the string '153' and gets its LENGTH, which is 3. We store 3 in v_num_digits.
  5. WHILE v_num > 0 LOOP: The loop begins and will run as long as v_num is positive.

Inside the loop:

  • Loop 1:
    • v_num = 153
    • v_remainder = MOD(153, 10) = 3
    • v_sum = 0 + POWER(3, 3) = 27
    • v_num = TRUNC(153 / 10) = 15 (Loop continues)
  • Loop 2:
    • v_num = 15
    • v_remainder = MOD(15, 10) = 5
    • v_sum = 27 + POWER(5, 3) = 27 + 125 = 152
    • v_num = TRUNC(15 / 10) = 1 (Loop continues)
  • Loop 3:
    • v_num = 1
    • v_remainder = MOD(1, 10) = 1
    • v_sum = 152 + POWER(1, 3) = 152 + 1 = 153
    • v_num = TRUNC(1 / 10) = 0 (Loop stops)

<!-- end list -->

  1. IF v_original_num = v_sum THEN: The program compares v_original_num (153) to the final v_sum (153).
  2. Since 153 = 153 is TRUE, it prints "153 is an Armstrong number."
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments