Skip to content
Related Articles

Related Articles

Improve Article

Sum of digits equal to a given number in PL/SQL

  • Last Updated : 12 Jul, 2018

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.

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:

--Take a number 
--sum all digit of the number 
--if sum digit is 25  
--then display all 
--Declaration block 
    --declare N variable 
    n NUMBER; 
    --declare B variable 
    m NUMBER; 
    --declare S variable 
    --S initialize with 0 
    s NUMBER := 0; 
    --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 
                               ||' '); 
        END IF; 
        s := 0; 
    END LOOP; 
--end loop 
--end program 



Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :