Sum of the first and last digit of a number in PL/SQL Last Updated : 11 Jul, 2025 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 n, the task is to find the sum of its first and last digit. Examples: Input: 14598. Output: Sum of the first and last digit is 9. Input: 987456. Output: Sum of the first and last digit is 15. Approach is to use the substr() function and store the first and last digit and then print their sum. Below is the required implementation: SQL DECLARE -- declare variables are A, B, C and S -- these are same datatype integer a INTEGER := 14598; b INTEGER := 0; C INTEGER := 0; s INTEGER; BEGIN IF a > 9 THEN c := Substr(a, 1, 1); b := Substr(a, Length(a), 1); s := b + c; ELSE s := a; END IF; dbms_output.Put_line('Sum of the first and last digit is ' ||s); END; -- Program End Output: Sum of the first and last digit is 9 Comment More infoAdvertise with us Next Article Sum of digits of a number in PL/ SQL S Shashank12 Follow Improve Article Tags : Misc SQL SQL-PL/SQL Practice Tags : Misc 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 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 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 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 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 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