Reputation: 1
How to write a program for multiplication table PL/SQL ask the user input a number
this is the code display just table without input
Declare
i NUMBER:=0;
x NUMBER;
Begin
loop
i := i+1;
x :=2*i;
dbms_output.put_line('2'||'x'||i||'='||x);
IF i >=10 THEN
EXIT ;
END IF;
END loop;
END;
/
Upvotes: 0
Views: 3592
Reputation: 7826
You could create the complete multiplication table with just sql and then select whatever you want without using PL/SQL ...
Here is the code: (all ran in SQL Developer)
WITH
nums AS
(
Select LEVEL "N" From Dual Connect By LEVEL <= 10
),
tbl AS
(
Select COL_N * N1 "N1", COL_N * N2 "N2", COL_N * N3 "N3", COL_N * N4 "N4", COL_N * N5 "N5",
COL_N * N6 "N6", COL_N * N7 "N7", COL_N * N8 "N8", COL_N * N9 "N9", COL_N * N10 "N10"
From ( SELECT n2.N "COL_N",n1. N "N", n1.N "ROW_N"
FROM nums n1
INNER JOIN nums n2 ON(1 = 1)
)
PIVOT ( MAX(N) FOR ROW_N IN(1 "N1", 2 "N2", 3 "N3", 4 "N4", 5 "N5", 6 "N6", 7 "N7", 8 "N8", 9 "N9", 10 "N10") )
Order By COL_N
)
... the content is
SELECT N1 "1", N2 "2", N3 "3", N4 "4", N5 "5", N6 "6", N7 "7", N8 "8", N9 "9", N10 "10" FROM tbl
1 2 3 4 5 6 7 8 9 10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
... and select with variable passed to:
SELECT LPAD(N1, 2, ' ') || ' x ' || LPAD(&&M_NUM, 2, ' ') || ' = ' || LPAD(N1 * &&M_NUM, 3, ' ') "RESULTS"
FROM tbl
-- with &&M_NUM = 6 results:
RESULTS
--------------
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
... Or you could get the results in one row (it's 6 again)
SELECT * FROM tbl WHERE N1 = &&M_NUM
N1 N2 N3 N4 N5 N6 N7 N8 N9 N10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
6 12 18 24 30 36 42 48 54 60
... or in columns ...
SELECT N1, &&M_NUM "MULTIPLYED _BY",
CASE &&M_NUM WHEN 2 THEN N2
WHEN 3 THEN N3
WHEN 4 THEN N4
WHEN 5 THEN N5
WHEN 6 THEN N6
WHEN 7 THEN N7
WHEN 8 THEN N8
WHEN 9 THEN N9
WHEN 10 THEN N10
END "RESULT"
FROM tbl
N1 MULTIPLYED _BY RESULT
---------- -------------- ----------
1 6 6
2 6 12
3 6 18
4 6 24
5 6 30
6 6 36
7 6 42
8 6 48
9 6 54
10 6 60
... or anything else you like...
Upvotes: 0
Reputation: 142968
A simple option (ran in SQL*Plus) is
SQL> set ver off
SQL> begin
2 for i in 1 .. 10 loop
3 dbms_output.put_line(&&par_number|| ' x ' || i ||' = '|| &&par_number * i);
4 end loop;
5 end;
6 /
Enter value for par_number: 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
PL/SQL procedure successfully completed.
SQL>
As Koen commented, depending on a client, substitution variable (&&par_number
) might need to be modified to a bind variable (:par_number
), or you'd enter it into a page item, or some other option.
More info you provide, better answer you get.
Upvotes: 1