- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
First, let’s brush-up about 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
Advertisements