0% found this document useful (0 votes)
2K views17 pages

Answers For Queries PL-SQL

The document contains 43 PL/SQL programs covering topics such as printing patterns, finding sums and factors of numbers, checking prime and Armstrong numbers, Fibonacci series, swapping values, date calculations, and more. The programs demonstrate the use of basic PL/SQL constructs like loops, conditional statements, functions and declare variables to perform various calculations and print outputs.

Uploaded by

javeedhunk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views17 pages

Answers For Queries PL-SQL

The document contains 43 PL/SQL programs covering topics such as printing patterns, finding sums and factors of numbers, checking prime and Armstrong numbers, Fibonacci series, swapping values, date calculations, and more. The programs demonstrate the use of basic PL/SQL constructs like loops, conditional statements, functions and declare variables to perform various calculations and print outputs.

Uploaded by

javeedhunk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING 2.

Write a program to print the numbers from 1 to 100 declare n number:=1; begin while n<=100 loop dbms_output.put_line(n); n:=n+1; end loop; end; 3.write a program to print the even numbers from 1 to 100 declare n number:=0; begin while n<=100 loop dbms_output.put_line(n); n:=n+2; end loop; end; 4.Write a program to print the odd numbers from 1 to 100 declare n number:=1; begin while n<=100 loop dbms_output.put_line(n); n:=n+2; end loop; end; 5.write a program for multiplication table declare f number(20):=&a; s number(20):=1; l number(20); begin while s<=20 loop l:=f*s; dbms_output.put_line(l||'='||f||'*'||s); s:=s+1; end loop; end; 6.write a program to find the sum of numbers from 1 to 100 declare n number(30):=1; n1 number(30):=0; begin while n<=100 loop n1:=n1+n; n:=n+1; end loop; dbms_output.put_line(n1); end; 7.Write a program to find the sum of all even numbers from 1 to 100 declare n number:=0;

n1 number:=2; begin while n<=100 loop n1:=n1+n; n:=n+2; end loop; dbms_output.put_line(n1); end; 8.Write a program to find the sum of all odd numbers from 1 to 100 declare n number:=1; n1 number:=1; begin while n<=100 loop n1:=n1+n; n:=n+1; end loop; dbms_output.put_line(n1); end; 9.Write a program to accept a number and find how many digits it contain declare n number(30):=14578455; m number(10); begin m:=length(n); dbms_output.put_line(m); end; 10.Write a program to accept a number and find the sum of the digits declare n number:=1544; m number:=0; o number:=0; begin while n!=0 loop m:=mod(n,10); n:=trunc(n/10); o:=o+m; end loop; dbms_output.put_line(o); end; 11.Write a program to accept a number and print it in reverse order declare n number(10):=143; m number(10):=0; j number(10):=0; begin while n!=0 loop j:=mod(n,10); n:=trunc(n/10); m:=(m*10)+j; end loop; dbms_output.put_line(m); end; 12.Write a program to accept a no and check whether it is Armstrong number or no t declare

x number(3); y number(3):=0; z number(3); begin x:=153; z:=x; while z>0 loop y:=y+power(mod (z,10),3); z:=trunc(z/10); end loop; if(y=x) then dbms_output.put_line('The given number ' || x || ' is an armstrong number'); else dbms_output.put_line('The given number ' || x || ' is not an armstrong number'); end if; end; 13.Write a porgram to generate all the Armstrong numbers from 1 to 1000 declare x number; y number; begin for i in 1..1000 loop x:=i; y:=0; loop y:=y + power(mod(x,10),3); x:=trunc(x/10); exit when x<=0; end loop; if (y = i) then dbms_output.put_line('Armstrong Number Is'||' - '||i); end if; end loop; end; 14.Write a program to generate all prime numbers between 1 to 100 declare a number:=1; b number:=1; c number; cnt number:=0; begin while a<=100 loop b:=1; cnt:=0; while b<=a loop c:=mod(a,b); if c=0 then cnt:=cnt+1; end if; b:=b+1; end loop; if cnt=2 then dbms_output.put_line('prime number'); else dbms_output.put_line('not a prime number'); end if;

a:=a+1; end loop; end; 15.Write a program to aceept a number and check whether it is prime number or no t declare a number:=5; b number:=0; c number; cnt number:=0; begin while b<=a loop c:=mod(a,b); if c=0 then cnt:=cnt+1; end if; b:=b+1; end loop; if cnt=2 then dbms_output.put_line('prime number'); else dbms_output.put_line('not a prime number'); end if; end; 16.Write a program to display the fibonacci series from 1 to 10 declare N number := &N; X number := 0; Y number := 1; Z number; begin DBMS_OUTPUT.PUT_LINE(X); DBMS_OUTPUT.PUT_LINE(Y); for i in 3..N LOOP Z := X + Y; DBMS_OUTPUT.PUT_LINE(Z); X := Y; Y := Z; end LOOP; end; 17.Write a program to aceept a number and print it in binary format declare x number:=&n; y number:=1; z varchar2(100); begin dbms_output.put_line('binary format'); while x!=0 loop z:=to_char(mod(x,2))||z; x:=trunc(x/2); end loop; dbms_output.put_line(z); end; 20.Write a program to aceept a number and display it in the Octal format declare x number:=&n; y number:=1;

z varchar2(100); begin dbms_output.put_line('binary format'); while x!=0 loop z:=to_char(mod(x,8))||z; x:=trunc(x/8); end loop; dbms_output.put_line(z); end; 18.Write a program to accept a number and find the factorial of the number declare x number:=5; y number:=1; z number:=1; begin while y<=x loop z:=z*y; y:=y+1; end loop; dbms_output.put_line(z); end ; 19.Find the factorials of numbers from 1 to 10 declare x number:=10; y number:=1; z number:=1; begin while y<=x loop z:=z*y; y:=y+1; dbms_output.put_line(z); end loop; end ; 21.Write a program to accept a number and print the multiplication tables upto 1 0 declare f number(20):=&n; s number(20):=1; l number:=0; begin while f!=0 loop s:=1; l:=0; while s<=20 loop l:=f*s; dbms_output.put_line(f||'*'||s||'='||l); s:=s+1; end loop; f:=f-1; end loop; end; 22.Write a program to accept the temp in Centigrade and convert it into Fahrenhe it(c=F-32/1.8) declare fah number:=0;

cen number:=&n; begin fah:=(1.8*cen)+32; dbms_output.put_line(fah); end; 23.Write a program to calculate the area of a triangle by accepting the 3 sides (s=(a+b+c)/2 area=sqrt(s*(s-a)*(s-b)*(s-c))) declare a number:=4; b number:=4; c number:=6; s number:=0; area NUMBER; begin s:=(a+b+c)/2; area:=sqrt(sqrt(s*(s-a)*(s-b)*(s-c))); dbms_output.put_line(area); end; 24.Write a program to calculate the area of a circle by accepting the radius and unit of measure Area=PI*r2 declare r number:=5; area NUMBER:=0; begin area:=(22/7)*r; dbms_output.put_line(area); end; 25.Write a program to calculate the perimeter of a circle(perimeter=2*PI*r) declare r number:=5; perimeter NUMBER:=0; begin perimeter:=(44/7)*r; dbms_output.put_line(perimeter); end; 26.Write a program to accept the 3 sides of the triangle and display the type of triangle declare a NUMBER:=&a; b number:=&b; c number:=&c; begin if (a=b AND b=c) then dbms_output.put_line('equilateral trialgle'); end if; if (a=b or b=c) THEN dbms_output.put_line('isosceles trialgle'); end if; if (a!=b and b!=c) then dbms_output.put_line('scalene triangle'); end if; end; 27.Write a program accept the value of A,B&C display which is greater declare a NUMBER:=&a; b number:=&b; c number:=&c; begin if (a>b AND a>c)

then dbms_output.put_line('a is big'); end if; if (b>a and b>c) THEN dbms_output.put_line('b is big'); end if; if (c>a and c>b) then dbms_output.put_line('c is big'); end if; end; 28.Write a program accept a string and check whether it is palindrome or not 29.Write a program aceepts the value of A,B and swap the nos and print the value s. a NUMBER:=&a; b number:=&b; temp NUMBER:=0; BEGIN dbms_output.put_line('before swaping a='||a||'b='||b); temp:=a; a:=b; b:=temp; dbms_output.put_line('After swaping a='||a||'b='||b); end; 30.Write a program to accept the values of A , B and swap the numbers and print the values without using third variable declare a number:=&a; b number:=&b; begin dbms_output.put_line('a='||a||'b='||b); a:=a+ b; b:=a- b; a:= a- b; dbms_output.put_line('a='||a||'b='||b); end; 31.Write a program to accept the side of a square and calculate the area area =a 2 declare s number:=&side; area number; begin area:=power(s,2); dbms_output.put_line(area); end; 32.Write a program to accept principle amount ,rate,time calculate the simple in terest si=(p*t*r)/100 declare si number; p number:=&p; r number:=&r; t number:=&t; begin si:=(p*r*t)/100; dbms_output.put_line(si); end; 33.write a program to aceept the principle amount,rate,time and find the compoun d interest ci=p*(1+r/100)n declare ci number; p number:=&p;

r number:=&r; n number:=&n; begin ci:=p*(1+r/100)*n; dbms_output.put_line(ci); end; 34.WAP to calculate the sum declare n number:=&n; s number:=0; f number:=1; begin for i in 1..n loop for j in 1..i loop f:=f*j; end loop; s:=s+f; f:=1; end loop; dbms_output.put_line(s); end; 35.WAP to calculate the sum declare n number:=&n; a number; s number(6,2):=0; begin for i in 1..n loop a:=1/i; s:=s+a; end loop; dbms_output.put_line(s); end; 36.WAP to calculate the sum declare n number:=&n; s number(6,2):=0; f number:=1; begin for i in 1..n loop for j in 1..i loop f:=f*j; end loop; s:=s+(1/f); end loop; dbms_output.put_line(s); end; 37.WAP to calculate the sum declare n number :=&n; s number(6,2):=0; f number :=1; begin for i in 1..n loop

of 1!+2!+......+n!

of 1+1/2+1/3+......+1/n .

of 1/1!+1/2!+.....+1/n!

of 1/1!+2/2!+......+n/n!

for j in 1..i loop f:=f*j; end loop; s:=s+(i/f); end loop; dbms_output.put_line(s); end; 38.Write a program to display the months between two dates of a year declare d date:='&D'; d1 date:='&D1'; begin while d < d1 loop dbms_output.put_line(to_char(d,'MONTH')); d:=add_months(d,1); end loop; end; 39.Write a program to accept the date and print the weekdays from the given date declare d date:='&D'; wd date; begin wd:=d+6; while d <= wd loop dbms_output.put_line(to_char(d,'DAY')); d:=d+1; end loop; end; 40.WAP to accept the date and print the weekdays from the given date along with date format declare d date:='&d'; wd date; begin wd:=d+6; while d <= wd loop dbms_output.put_line(to_char(d,'day')||d); d:=d+1; end loop; end; 41.Write a program to accept a year and check whether it is leap year or not declare y number:=&y; r number; begin if mod(y,4)=0 and mod(y,100)!=0 or mod(y,400)=0 then dbms_output.put_line(y ||' IS A LEAP YEAR'); else dbms_output.put_line(y ||' IS NOT A LEAP YEAR'); end if; end; 42.Write a program to accept a year and display all sundays along with the date declare

y number(4):=&yyyy; a date; b date; i number(2):=1; begin a:=to_date('01-JAN-'||y,'DD-MON-YYYY'); b:=last_day(add_months(a,11)); while a <= b loop if to_char(a,'D')=1 then dbms_output.put_line(lpad(i,2,'0')||'-'||upper(to_char(a,'DAY'))||a); i:=i+1; end if; a:=a+1; end loop; end; 43.WAP to accept a string and count how many vowels present in the string declare v varchar2(300):='&V'; cnt number(5):=0; c char; begin for i in 1..length(v) loop c:=substr(v,i,1); if c in ('A','E','I','O','U') then cnt:=cnt+1; end if; end loop; dbms_output.put_line(cnt); end; 44.Write a program to accept a year and check whether it is leap year or not. If it is leap year then display how many sundays present in that year. declare d date:='&YEAR'; y varchar2(20); cnt number(5):=0; v varchar2(20); begin y:=to_char(d,'YYYY'); d:=to_date('01-JAN-'||y); if mod(y,4)=0 and mod(y,100)!=0 or mod(y,400)=0 then for i in 1..366 loop v:=to_char(d,'D'); if v=1 then cnt:=cnt+1; end if; d:=d+1; dbms_output.put_line(cnt); end loop; end; 45.Write a program to accept a char and check it is vowel or consonant declare x char:='&x'; begin if x='a' or x='e' or x='i' or x='o' or x='u' then dbms_output.put_line('vowel'); else dbms_output.put_line('consonant');

end if; end; 46.WAP to accept A,B,C & D check whether it is Ramanujan number or not declare m number:=&m; n number:=&n; o number:=&o; p number:=&p; begin if (m*m*m)+(n*n*n)=(o*o*o)+(p*p*p) then dbms_output.put_line('ramanujan number'); else dbms_output.put_line('not ramanujan number'); end if; end; 47.WAP to accept the CMR & LMR & find out the total bill amount i)0-100 units Rs.50 per unit ii)101-200n units Rs.o.25 per unit iii)>200 units Rs.1.25 per unit declare lmr number(5):=&lmr; cmr number(5):=&cmr; tot number(5):=0; bill number(7,2):=0; begin tot:=cmr-lmr; if tot <= 100 then bill:=tot*.50; elsif tot > 100 and tot <= 200 then bill:=(100*.50)+((tot-100)*.75); else bill:=(100*.50)+(100*.75)+(tot-200)*1.25; end if; dbms_output.put_line(tot); dbms_output.put_line(bill); end; 48.WAP or accept marks of 3 subject as i/p and calculate the total marks and div ision of a student i) If totmark>=60 then division is First ii) If totmark <60 and totmark>=50 then division is second iii) If totmark< 50 and >=35 then division is third iv) If totmark< 35 then fail declare x number(2):=&m1; y number(2):=&m2; z number(2):=&m3; totmark number(5,2); ave number(5,2):=0; begin totmark:=x+y+z; ave:=totmark/3; if ave>=60 then dbms_output.put_line('first'); elsif ave<60 and ave>=50 then dbms_output.put_line('second'); elsif ave<50 and ave>=35 then dbms_output.put_line('third'); else dbms_output.put_line('fail');

end if; end; 49.WAP to accept a number and print its multiplication table horizontally declare j number:=&j; v varchar2(1000); k number(3); begin for i in 1..10 loop k:=j*i; v:=v||j||'*'||i||'='||k||' '; end loop; dbms_output.put_line(v); end; 50.WAP to accept a string and print it in reverse order declare str varchar2(100):='&sTR'; str1 varchar2(100); n number(5); l varchar2(20); begin n:=length(str); for i in 1..n loop l:=substr(str,i,1); str1:=l||str1; end loop; dbms_output.put_line(str1); end; 51.Write a program to accept a number and find out the sum of first and last dig its declare a number:=&a; b number(5):=0; c number(5):=0; s number(5); begin if a>9 then c:=substr(a,1,1); b:=substr(a,length(a),1); s:=b+c; else s:=a; end if; dbms_output.put_line(s); end; 52.WAP to accept the basic salary and find out the ta,da,hra,lic and gs ta 20% of basic, da 10% of basic, hra 30% of basic, lic 5% of basic declare s number(6,2):=&s; ta number(6,2); da number(6,2); hra number(6,2); gs number(6,2); lic number(6,2); ns number(8,2); begin ta:=s*(20/100); hra:=s*(30/100);

da:=s*(10/100); lic:=s*(5/100); gs:=ta+hra+da; ns:=gs-lic; dbms_output.put_line(s); dbms_output.put_line(gs); dbms_output.put_line(ns); end; 53.WAP to accept the length and breadth of a rectangle and find out the perimete r declare l number:=&l; b number:=&b; a number; begin a:=2*(l+b); dbms_output.put_line('THE PERIMETER OF RECTANGLE IS '||a); end; 54.WAP to accept the cost price and selling price of an item and find the loss o r profit declare cp number:=&cp; sp number:=&sp; p number; begin if cp < sp then p:=sp-cp; dbms_output.put_line('PROFIT'); else p:=cp-sp; dbms_output.put_line('LOSS'); end if; end; 55.Writ a program to generate the following series 53 53 53 53 53 43 43 43 43 33 33 33 23 23 13 declare v number(30); begin for i in reverse 1..6 loop for j in 1..i loop v:=v||i; end loop; dbms_output.put_line(v); v:=null; end loop; end; 56.WAP to accept a no in binary format and print it in decimal format declare n varchar2(20):=&n; pro number:=0; l varchar2(10); begin for i in 1..length(n) loop l:=substr(n,i,1);

pro:=pro+l*power(2,length(n)-i); end loop; dbms_output.put_line('DECIMAL NUMBER'); end; 57.WAP to accept two nos as input and find one no is raised to another one (with out using any function) set serveroutput on; declare a number:=&a; b number:=&b; r number:=1; begin for i in 1..b loop r:=r*a; end loop; dbms_output.put_line('a raised to power of b is '||r); end; 58.WAP to accept a sentence and count the no of chars in that sentence declare str varchar2(100):='&str'; i number; begin i:=length(str); dbms_output.put_line(i); end; 59.WAP to accept two strings and display the large one among those declare str1 varchar2(100):='&str1'; str2 varchar2(100):='&str2'; begin if length(str1) > length(str2) then dbms_output.put_line(str1 ||' IS GREATER'); elsif length(str1) < length(str2) then dbms_output.put_line(str2 ||' IS GREATER'); else dbms_output.put_line('BOTH STRINGS ARE EQUAL'); end if; end; 60.WAP to display all the nos whose sum of digits is 9 from 1 to 9999 declare n number; m number; s number:=0; begin for i in 1..9999 loop n:=i; while n>0 loop m:=mod(n,10); s:=s+m; n:=trunc(n/10); end loop; if s=9 then dbms_output.put_line(i||' '); end if; s:=0; end loop; end;

61.WAP to accept a no and find the sum in a single digit declare n number:=&n; s number:=0; begin while length(n)>1 loop for i in 1..length(n) loop s:=s+substr(n,i,1); end loop; n:=s; s:=0; end loop; dbms_output.put_line('THE SUM IN SINGLE DIGIT IS '||n); end; 62.Enter the no of days and find out the no of years and no of days and months declare d number:=&d; y number; m number; begin y:=trunc(d/365); m:=trunc(mod(d,365)/30); d:=mod(mod(d,365),30); dbms_output.put_line(y||' YEARS '||m||' MONTHS '||d||' DAYS'); end; 63.WAP to accept the date and print all the weekdays along with the given date declare d date:='&D'; v varchar2(20); begin for i in 1..7 loop v:=to_char(d,'DAY')||d; dbms_output.put_line(v); d:=d+1; end loop; end; 64.WAP while purchasing certain items, discount of each is as follows i) If qty purchased > 1000 discount is 20% ii) If the qty and price per item are input then calculate the expenditure declare qty number(5):=&qty; up number:=&up; dis number:=0; amt number; bill number; begin bill:=qty*up; if bill > 1000 then dis:=bill*20/1000; end if; amt:=bill-dis; dbms_output.put_line('AMOUNT IS '||amt); end; 65.Write a program to accept a string and count the no of individual chars declare v varchar2(100):='&V'; v1 varchar2(100);

lb number; la number; diff number; c char; n number(5):=0; begin v1:=v; while length(v1)>0 loop c:=substr(v1,1,1); lb:=length(v1); v1:=replace(v1,c); la:=nvl(length(v1),0); diff:=lb-la; if ascii(c)=32 then dbms_output.put_line('SPACE'||' EXISTS '||diff||' TIMES'); else dbms_output.put_line(c||' EXISTS '||diff||' TIMES'); end if; n:=n+diff; end loop; dbms_output.put_line('TOTAL LENGTH OF THE GIVEN STRING '||v||'='||n); end; 66.Write a program to display all combination of 1,2,&3 declare i number; j number; k number; begin for i in 1..3 loop for j in 1..3 loop for k in 1..3 loop dbms_output.put_line(i||j||k); end loop; end loop; end loop; end; 67.Write a program to find out the series 12+22+32+42+....++n2 68.Write a program to accept the time in HH & MIN format and find the total seco nds declare h number:=&hour; m number:=&minute; s number(10):=0; begin s:=(h*60*60)+(m*60); dbms_output.put_line(s); end; 69.WAP to accept the distance between two cities in km and convert into mts ,cm & ft declare d number:=&d; m number; f number; cm number; begin m:=d*1000;

f:=m*3.28; cm:=m*100; dbms_output.put_line('meters are' ||m); dbms_output.put_line('foots are' ||f); dbms_output.put_line('centimeters are' ||cm); end; 70.Write a program to find the series x+x2/2!+x3/3!+.....+xn/n! declare n number:=&n; x number:=&x; s number:=0; f number:=1; begin for i in 1..n loop for j in 1..i loop f:=f*j; end loop; s:=round(s+(power(x,i)/f),3); f:=1; end loop; dbms_output.put_line(s); end;

You might also like