Sum of digits 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 and task is to find the sum of digits of the number.
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:
sum of digits = 21
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.