Sum of digits of a number in PL/ SQL Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and task is to find the sum of digits of the number. Examples: Input: 123456 Output: 21 Input: 9874 Output: 28 Approach is to take a number and getting each digit with MOD function and summing it up. Below is the required implementation: SQL DECLARE --Declare variable n, temp_sum -- and r of datatype number n INTEGER; temp_sum INTEGER; r INTEGER; BEGIN n := 123456; temp_sum := 0; -- here we check condition with the help of while loop -- here <> symbol represent for not null WHILE n <> 0 LOOP r := MOD(n, 10); temp_sum := temp_sum + r; n := Trunc(n / 10); END LOOP; dbms_output.Put_line('sum of digits = ' || temp_sum); END; -- Program End Output: sum of digits = 21 Comment More infoAdvertise with us Next Article PHP | Sum of digits of a number S Shashank12 Follow Improve Article Tags : Misc SQL SQL-PL/SQL Practice Tags : Misc Similar Reads PHP | Sum of digits of a number This is a simple PHP program where we need to calculate the sum of all digits of a number. Examples: Input : 711 Output : 9 Input : 14785 Output : 25 In this program, we will try to accept a number in the form of a string and then iterate through the length of the string. While iterating we will ext 1 min read Sum of the first and last digit of a number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number n, the task is to find the sum of its fi 1 min read Sum of the first and last digit of a number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number n, the task is to find the sum of its fi 1 min read Sum of digits equal to a given number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and range and the task is to display all 2 min read Sum of digits equal to a given number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and range and the task is to display all 2 min read Sum Of Two Numbers in PL/SQL Prerequisite - PL/SQL introductionIn PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Here, first, we take three variables x, y, and z and ass 2 min read Like