PL/SQL Program to Add Two Numbers
Here you will learn that how to create PL/SQL program to add two numbers or integers value and store that in third variable.
PL/SQL Program to Add Two Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | DECLARE -- Declare variable num1 NUMBER; num2 NUMBER; sum NUMBER; BEGIN -- Assign values to variables num1 := 10; num2 := 20; -- Add two numbers and store value in sum variable sum := num1 + num2; -- Print result DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' and ' || num2 || ' is: ' || sum); END; / |
Output
The sum of 10 and 20 is: 30
Explanation
DECLARE: It is used to declares the variables (num1, num2, and sum).
BEGIN: It Starts the executable part of PL/SQL block.
DBMS_OUTPUT.PUT_LINE: It prints the output/result.
END: It is used to marks the end of the PL/SQL block.
This program is used to add num1 and num2 values, and stores the result in sum variable, then prints the sum.
Note:- If you want you can change the values of num1 and num2 as needed.
PL/SQL Program to Add Two Numbers by user input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DECLARE num1 NUMBER; num2 NUMBER; sum NUMBER; BEGIN -- get user input 1 DBMS_OUTPUT.PUT_LINE('Enter the first number:'); num1 := &num1; -- get user input 2 DBMS_OUTPUT.PUT_LINE('Enter the second number:'); num2 := &num2; -- addition sum := num1 + num2; -- Print result DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' and ' || num2 || ' is: ' || sum); END; / |
Output
Enter the first number: 10
Enter the second number: 20
The sum of 10 and 20 is: 30
Check out our other PL/SQL programs examples