DECLARE
num NUMBER := 12345;
sum NUMBER := 0;
BEGIN
WHILE (num > 0) LOOP
sum := sum + MOD(num, 10);
num := TRUNC(num / 10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('The sum of digits is ' || sum);
END;
In this code, the variable num is assigned the value of the number whose digits we want to sum, and the variable
sum is initialized to 0. The code uses a while loop to extract the digits of the number one by one and add them to the sum.
Given below is the program to find the sum of digits of a program using pl/sql.
set serveroutput on
declare //
declaring all variables
n number;
sum number:=0;
i number;
begin
n:=123; // initializig variables
i:=0;
while i<n LOOP
r := MOD(n, 10); // finding last digit of the number
sum := sum + r; // adding last digit to the sum
n := Trunc(n / 10); // removing last digit of the number
end LOOP;
dbms_output.put_line('Sum of digits of number '||n||' is '||sum||'.');
end;
/
# OUPUT - Sum of digits of number 123 is 6.
This program uses a loop to find the last digit of a number by calculating the remainder when the number is divided by 10. After finding the remainder, it adds it to the sum and removes the digit in the unit's place by dividing the number by 10. This process is repeated until the number becomes less than or equal to 0. Finally, the program returns the sum as the result of the calculation.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In this code, the variable num is assigned the value of the number whose digits we want to sum, and the variable sum is initialized to 0. The code uses a while loop to extract the digits of the number one by one and add them to the sum.
Given below is the program to find the sum of digits of a program using pl/sql.
This program uses a loop to find the last digit of a number by calculating the remainder when the number is divided by 10. After finding the remainder, it adds it to the sum and removes the digit in the unit's place by dividing the number by 10. This process is repeated until the number becomes less than or equal to 0. Finally, the program returns the sum as the result of the calculation.