Sum of digits of a number in PL/ SQL Last Updated : 09 Jul, 2018 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 Sum of the first and last digit of a number in PL/SQL 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 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 Sum of digits of a given number to a given power Given a number, we need to find the sum of all the digits of a number which we get after raising the number to a specified power.Examples: Input: number = 5, power = 4 Output: 13 Explanation: Raising 5 to the power 4 we get 625. Now adding all the digits = 6 + 2 + 5 Input: number = 9, power = 5 Outp 3 min read Finding sum of first n natural numbers 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 positive integer n and the task is to find the 3 min read Like