Sum of digits equal to a given number in PL/SQL Last Updated : 12 Jul, 2018 Comments Improve Suggest changes 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 range and the task is to display all the numbers whose sum of digits is equal to the given number. Examples: Input: x = 23 Output: 599 689 698 779 788 797 869 878 887 896 959 968 977 986 995 (Note: range->1 to 999) Input: x=12 Output: 39 48 57 66 75 84 93 (Note: range->1 to 100) Approach is to take a number, find all the possible numbers in the given range and sum all the digits of number and if sum of digits is equal to the number, then print that number. Below it's implementation: SQL --Take a number --sum all digit of the number --if sum digit is 25 --then display all --Declaration block DECLARE --declare N variable n NUMBER; --declare B variable m NUMBER; --declare S variable --S initialize with 0 s NUMBER := 0; BEGIN --Code block --loop run until max 999 to min 1 FOR i IN 1..999 LOOP n := i; WHILE n > 0 LOOP --logic of digit sum m := MOD(n, 10); s := s + m; n := Trunc(n / 10); END LOOP; IF s = 25 THEN --digit sum to be same with 25 --Display in result dbms_output.Put_line(i ||' '); END IF; s := 0; END LOOP; --end loop END; --end program Output: 799 889 898 979 988 997 Comment More infoAdvertise with us Next Article Sum of digits equal to a given number in PL/SQL J jit_t Follow Improve Article Tags : Mathematical DSA SQL-PL/SQL Practice Tags : Mathematical Similar Reads 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 1 min read Print all n-digit numbers whose sum of digits equals to given sum Given number of digits n, print all n-digit numbers whose sum of digits adds upto given sum. Solution should not consider leading 0âs as digits.Examples: Input: N = 2, Sum = 3Output: 12 21 30Input: N = 3, Sum = 6Output: 105 114 123 132 141 150 204 213 222 231 240 303 312 321 330 402 411 420 501 510 10 min read Count of n digit numbers whose sum of digits equals to given sum Given two integers n and sum, the task is to find the count of all n digit numbers with sum of digits equal to sum. Note: Leading 0's are not counted as digits. If there exist no n digit number with sum of digits equal to given sum, print -1.Example: Input: n = 2, sum= 2Output: 2Explanation: The num 15+ min read Count numbers (smaller than or equal to N) with given digit sum Given a number N and a sum S, find the count of numbers upto N that have digit sum equal to S. Examples: Input : N = 100, S = 4 Output : 5 Upto 100 only 5 numbers(4, 13, 22, 31, 40) can produce 4 as their sum of digits. Input : N = 1000, S = 1 Output : 4 Upto 1000 only 4 numbers(1, 10, 100 and 1000) 8 min read Find a number x such that sum of x and its digits is equal to given n. Given a positive number n. We need to find a number x such that sum of digits of x to itself is equal to n. If no such x is possible print -1.Examples: Input : n = 21 Output : x = 15 Explanation : x + its digit sum = 15 + 1 + 5 = 21 Input : n = 5 Output : -1 We iterate from 1 to n and for each inter 5 min read Like