# Sum of the first and last digit of a number in PL/SQL

In this problem, we are given a number n. Our task is to create a program to find the sum of the first and last digit of a number in PL/SQL.

PL/SQL is a combination of SQL along with the procedural features of programming languages.

Let’s take an example to understand the problem,

Input − n = 31415

Output − 8

Explanation − first digit = 3 , last digit = 5. Sum = 8

To, solve this problem, we will extract the first and last digit to number n. And the print their sum.

The first and last digits are extracted using the substr() function.

## Example

Program to illustrate the working of our solution,

DECLARE
n INTEGER := 31415;
fistDigit INTEGER := 0;
lastDigit INTEGER := 0;
s INTEGER;
BEGIN
IF a > 9 THEN
lastDigit:= Substr(n, 1, 1);
fistDigit := Substr(n, Length(n), 1);
s := fistDigit + lastDigit;
ELSE
s := n;
END IF;
dbms_output.Put_line('Sum of the first digit and last digit of the number is ' ||s);
END;
-- Program End

## Output

Sum of the first digit and last digit of the number is 8

Updated on: 06-Aug-2020

659 Views