0% found this document useful (0 votes)
64 views6 pages

PL/SQL Program for Adding Two Numbers

The document contains 11 PL/SQL code examples that demonstrate various programming concepts such as: 1. Adding two numbers 2. Checking if a number is prime 3. Calculating the factorial of a number 4. Printing the multiplication table of a number 5. Reversing a number 6. Printing the Fibonacci series 7. Checking if a number is odd or even 8. Reversing a string 9. Checking if a number is a palindrome 10. Swapping two numbers using a temporary variable 11. Finding the greatest of three numbers

Uploaded by

Giri vaannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views6 pages

PL/SQL Program for Adding Two Numbers

The document contains 11 PL/SQL code examples that demonstrate various programming concepts such as: 1. Adding two numbers 2. Checking if a number is prime 3. Calculating the factorial of a number 4. Printing the multiplication table of a number 5. Reversing a number 6. Printing the Fibonacci series 7. Checking if a number is odd or even 8. Reversing a string 9. Checking if a number is a palindrome 10. Swapping two numbers using a temporary variable 11. Finding the greatest of three numbers

Uploaded by

Giri vaannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

PL/SQL Program To Add Two Numbers

1 Declare
2 Var1 integer;
3 Var2 integer;
4 Var3 integer;
5 Begin
6 Var1:=&var1;
7 Var2:=&var2;
8 Var3:=var1+var2;
9 Dbms_output.put_line(var3);
10 End;
11 /

[Link] or not

1 declare
2 n number;
3 i number;
4 flag number;
5
6 begin
7 i:=2;
8 flag:=1;
9 n:=&n;
10
11 for i in 2..n/2
12 loop
13 if mod(n,i)=0
14 then
15 flag:=0;
16 exit;
17 end if;
18 end loop;
19
20 if flag=1
21 then
22 dbms_output.put_line('prime');
23 else
24 dbms_output.put_line('not prime');
25 end if;
26 end;
27 /

3.

1 declare
2 n number;
3 fac number:=1;
4 i number;
5
6 begin
7 n:=&n;
8
9 for i in 1..n
10 loop
11 fac:=fac*i;
12 end loop;
13
14 dbms_output.put_line('factorial='||fac);
15 end;
16 /

4. table of a number
declare
n number;
i number;
begin
n:=&n;
for i in 1..10
loop
dbms_output.put_line(n||' x '||i||' = '||n*i);
end loop;
end;
/
Output
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

5. reverse of a number.
declare
n number;
i number;
rev number:=0;
r number;

begin
n:=&n;

while n>0
loop
r:=mod(n,10);
rev:=(rev*10)+r;
n:=trunc(n/10);
end loop;

dbms_output.put_line('reverse is '||rev);

end;
/

Output
Enter value for n: 4578
old 8: n:=&n;
new 8: n:=4578;
reverse is 8754

6. reverse of a number.
first number:=0;
second number:=1;
third number;
n number:=&n;
i number;

begin
dbms_output.put_line('Fibonacci series is:');
dbms_output.put_line(first);
dbms_output.put_line(second);

for i in 2..n
loop
third:=first+second;
first:=second;
second:=third;
dbms_output.put_line(third);
end loop;
end;
/

Output

Enter value for n: 6


old 5: n number:=&n;
new 5: n number:=6;
Fibonacci series is:
0
1
1
2
3
5
8

7. odd or even
declare
n number:=&n;

begin
if mod(n,2)=0
then
dbms_output.put_line('number is even');
else
dbms_output.put_line('number is odd');
end if;
end;
/
Enter value for n: 7
old 2: n number:=&n;
new 2: n number:=7;
number is odd

declare
str1 varchar2(50):='&str';
str2 varchar2(50);
len number;
i number;

begin
len:=length(str1);

for i in reverse 1..len


loop
str2:=str2 || substr(str1,i,1);
end loop;

dbms_output.put_line('Reverse of String is:'||str2);


end;
/

Output
Enter value for str: hello world
old 2: str1 varchar2(50):=’&str’;
new 2: str1 varchar2(50):=’hello world’;
Reverse of String is:dlrow olleh

8. Pl/SQL Program for Palindrome Number

1 declare
2 n number;
3 m number;
4 rev number:=0;
5 r number;
6
7 begin
8 n:=12321;
9 m:=n;
10
11 while n>0
12 loop
13 r:=mod(n,10);
14 rev:=(rev*10)+r;
15 n:=trunc(n/10);
16 end loop;
17
18 if m=rev
19 then
20 dbms_output.put_line('number is palindrome');
21 else
22 dbms_output.put_line('number is not palindrome');
23 end if;
24 end;
25 /
Output
number is palindrome

9. Method 1: Using Temporary Variable

1 declare
2 a number;
3 b number;
4 temp number;
5
6 begin
7 a:=5;
8 b:=10;
9
10 dbms_output.put_line('before swapping:');
11 dbms_output.put_line('a='||a||' b='||b);
12
13 temp:=a;
14 a:=b;
15 b:=temp;
16
17 dbms_output.put_line('after swapping:');
18 dbms_output.put_line('a='||a||' b='||b);
19
20 end;
21 /
Output
before swapping:
a=5 b=10
after swapping:
a=10 b=5

10.

PL/SQL Program for Armstrong Number

1 declare
2 n number:=407;
3 s number:=0;
4 r number;
5 len number;
6 m number;
7
8 begin
9 m:=n;
10
11 len:=length(to_char(n));
12
13 while n>0
14 loop
15 r:=mod(n,10);
16 s:=s+power(r,len);
17 n:=trunc(n/10);
18 end loop;
19
20 if m=s
21 then
22 dbms_output.put_line('armstrong number');
23 else
24 dbms_output.put_line('not armstrong number');
25 end if;
26
27 end;
28 /
Output
armstrong number

11.
eclare
a number:=10;
b number:=12;
c number:=5;
begin
dbms_output.put_line('a='||a||' b='||b||' c='||c);
if a>b AND a>c
then
dbms_output.put_line('a is greatest');
else
if b>a AND b>c
then
dbms_output.put_line('b is greatest');
else
dbms_output.put_line('c is greatest');
end if;
end if;
end;
/
Output
a=10 b=12 c=5
b is greatest

Common questions

Powered by AI

The PL/SQL program reverses a string by first determining its length, then iterating backward from the last character to the first. In each iteration, it concatenates the current character to a new string, effectively creating the reverse of the original string. This is done using a for loop in reverse order and the substr function to extract characters .

The program uses a simple modulus operation to determine if a number is even or odd. If the number modulo 2 is equal to 0, the number is classified as even. Otherwise, it is classified as odd. This method utilizes the inherent properties of division and remainder .

In PL/SQL, a Fibonacci series is created by initializing the first two numbers in the series and then using a loop to calculate subsequent numbers. Each new number is the sum of the two preceding ones. This is achieved using variables to store two consecutive numbers, and updating them in each iteration of the loop until the series reaches the required length .

Swapping is done by storing the value of one variable in a temporary variable, then assigning the second variable’s value to the first, and finally, the value in the temporary variable is assigned to the second variable. This effectively swaps the values of the two variables without losing any data .

The program converts the string to its reverse using a loop, similar to the reverse string method, and then compares the original string with its reverse. If both are equal, the string is a palindrome. This ensures that the string reads the same forwards and backwards .

The PL/SQL program checks if a number is prime by setting an initial flag to 1, assuming the number is prime. It iterates from 2 up to n/2; if any division results in a remainder of 0, the flag is set to 0, indicating the number is not prime, and exits the loop. If the flag remains 1 after the loop, the number is confirmed as prime .

To calculate the factorial, the program initializes a variable to 1. It then iterates from 1 to n, multiplying this variable by each iterate value, accumulating the product. Once the loop completes, the resulting value is the factorial of the given number .

To modify the program to add three numbers, a new variable (say Var4) should be declared. Initialize Var4 with user input similarly to Var1 and Var2. Modify the existing operation to Var3 := Var1 + Var2 + Var4 to incorporate the third number and display the result using dbms_output.put_line .

To determine if a number is an Armstrong number, the program calculates the length of the number. It then iterates through each digit, raising it to the power of the number's length and accumulates these values. If the total matches the original number, it is an Armstrong number, otherwise, it is not .

A number is classified as an Armstrong number if the sum of its digits, each raised to the power of the total number of digits, equals the number itself. The PL/SQL program verifies this by performing calculations on each digit and comparing the sum to the original number .

You might also like