Advertisement
fsoc131y

palindrome

Oct 17th, 2023 (edited)
1,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 7.87 KB | Source Code | 0 0
  1. CURSOR EXPERIMENT:
  2. 0. Program 0: Program TO access tables
  3. DECLARE
  4. job_count NUMBER;
  5. emp_count NUMBER;
  6. BEGIN
  7. SELECT COUNT(DISTINCT id) INTO job_count FROM emp22;
  8. SELECT COUNT(*) INTO emp_count FROM emp22;
  9. DBMS_OUTPUT.put_line('job COUNT='||job_count);
  10. DBMS_OUTPUT.put_line('employee COUNT='||emp_count);
  11. END;
  12. /
  13.  
  14. 1. Program 01:
  15. DECLARE
  16. BEGIN
  17. FOR emp IN(SELECT id,name FROM emp22)
  18. LOOP
  19. DBMS_OUTPUT.put_line('Employee id AND employee name are '||emp.id||'AND '||emp.name);
  20. END LOOP;
  21. END;
  22. /
  23.  
  24. 2. Program 02:
  25. DECLARE
  26. CURSOR emp2 IS SELECT id,name,salary FROM emp22 WHERE role=’manager’;
  27. BEGIN
  28. FOR emrec IN emp2
  29. LOOP
  30. UPDATE emp22 SET salary=emrec.salary+20000 WHERE id=emrec.id;
  31. END LOOP;
  32. END;
  33. /
  34.  
  35. 3. Program 03:
  36. DECLARE
  37. CURSOR cur1 IS SELECT id,salary FROM emp22 WHERE id=102;
  38. ecode emp22.id % TYPE;
  39. esal emp.salary % TYPE;
  40. BEGIN
  41. OPEN cur1;
  42. LOOP
  43. FETCH cur1 INTO ecode,esal;
  44. EXIT WHEN cur1 % notFound;
  45. DBMS_OUTPUT.put_line('Employee code AND salary are '||ecode||'AND '||esal);
  46. END LOOP;
  47. CLOSE cur1;
  48. END;
  49. /
  50.  
  51. 4. Program 04:
  52. DECLARE
  53. CURSOR cur1 IS SELECT id,salary FROM emp22 WHERE salary=60000;
  54. ecode emp22.id % TYPE;
  55. esal emp.salary % TYPE;
  56. BEGIN
  57. OPEN cur1;
  58. LOOP
  59. FETCH cur1 INTO ecode,esal;
  60. EXIT WHEN cur1 % notFound;
  61. DBMS_OUTPUT.put_line('Employee code AND salary are '||ecode||'AND '||esal);
  62. END LOOP;
  63. CLOSE cur1;
  64. END;
  65. /
  66.  
  67.  
  68. ----------------------------------------------------------------------------------------------------------------------------
  69. Complete the following list OF simple programs IN PL/SQL - PREVIOUS EXPERIMENTS
  70.  
  71. 1. Addition OF two numbers
  72. SET serveroutput ON
  73. edit
  74. edit file name
  75. DECLARE
  76. a int;
  77. b int;
  78. c int;
  79. BEGIN
  80. a:=&a;
  81. b:=&b;
  82. c:=a+b;
  83. DBMS_OUTPUT.put_line('Sum of a and b is '||c);
  84. END;
  85. /
  86.  
  87. output:
  88. SQL> @add
  89. Enter VALUE FOR a: 12
  90. old   6: a:=&a;
  91. NEW   6: a:=12;
  92. Enter VALUE FOR b: 58
  93. old   7: b:=&b;
  94. NEW   7: b:=58;
  95. SUM OF a AND b IS 70
  96.  
  97. PL/SQL PROCEDURE successfully completed.
  98.  
  99. 2. Area OF Circle
  100.  
  101. DECLARE
  102. r NUMBER;
  103. pi NUMBER := 3.14;
  104. area NUMBER;
  105. BEGIN
  106. r:=&r;
  107. area := pi * r * r;
  108. DBMS_OUTPUT.put_line('Area of a circle: '||area);
  109. END;
  110. /
  111.  
  112. OUTPUT:
  113.  
  114. SQL> edit area
  115.  
  116. SQL> @area
  117. Enter VALUE FOR r: 5
  118. old   6: r:=&r;
  119. NEW   6: r:=5;
  120. Area OF a circle: 78.5
  121.  
  122. PL/SQL PROCEDURE successfully completed.
  123.  
  124.  
  125. 3. Biggest OF three numbers
  126.  
  127. DECLARE
  128. a int;
  129. b int;
  130. c int;
  131. BEGIN
  132. a:=&a;
  133. b:=&b;
  134. c:=&c;
  135. IF(a>b AND a>c) THEN
  136. DBMS_OUTPUT.put_line('a is greatest');
  137. ELSIF(b>a AND b>c) THEN
  138. DBMS_OUTPUT.put_line('b is greatest');
  139. ELSIF(c>b) THEN
  140. DBMS_OUTPUT.put_line('c is greatest');
  141. ELSE
  142. DBMS_OUTPUT.put_line('a=b=c');
  143. END IF;
  144. END;
  145. /
  146.  
  147. OUTPUT:
  148. SQL> SET serveroutput ON
  149. SQL> edit
  150. SP2-0107: Nothing TO save.
  151. SQL> edit tgreat
  152.  
  153. SQL> @tgreat
  154. Enter VALUE FOR a: 12
  155. old   6: a:=&a;
  156. NEW   6: a:=12;
  157. Enter VALUE FOR b: 52
  158. old   7: b:=&b;
  159. NEW   7: b:=52;
  160. Enter VALUE FOR c: 32
  161. old   8: c:=&c;
  162. NEW   8: c:=32;
  163. b IS GREATEST
  164.  
  165. PL/SQL PROCEDURE successfully completed.
  166.  
  167. 4. Printing 1 TO n NATURAL numbers using
  168.          (i) simple LOOP
  169.          (ii) WHILE LOOP
  170.          (iii) FOR LOOP
  171.  
  172. DECLARE
  173. a int;
  174. b int:=1;
  175. i int;
  176. c int :=1;
  177. BEGIN
  178. a:=&a;
  179. DBMS_OUTPUT.put_line('Printing using simple loop');
  180. LOOP
  181. EXIT WHEN b>a;
  182. DBMS_OUTPUT.put_line(b);
  183. b:=b+1;
  184. END LOOP;
  185. DBMS_OUTPUT.put_line('Printing using while loop');
  186. WHILE (c<=a) LOOP
  187. DBMS_OUTPUT.put_line(c);
  188. c:=c+1;
  189. END LOOP;
  190. DBMS_OUTPUT.put_line('Printing using for loop');
  191. FOR b IN 1..a LOOP
  192. DBMS_OUTPUT.put_line(b);
  193. END LOOP;
  194. END;
  195. /
  196.  
  197. OUTPUT:
  198.  
  199. SQL> edit LOOP
  200.  
  201. SQL> @LOOP
  202. Enter VALUE FOR a: 5
  203. old   7: a:=&a;
  204. NEW   7: a:=5;
  205. Printing using simple LOOP
  206. 1
  207. 2
  208. 3
  209. 4
  210. 5
  211. Printing using WHILE LOOP
  212. 1
  213. 2
  214. 3
  215. 4
  216. 5
  217. Printing using FOR LOOP
  218. 1
  219. 2
  220. 3
  221. 4
  222. 5
  223.  
  224. PL/SQL PROCEDURE successfully completed.
  225.  
  226.  
  227. 5.  Printing 1 TO n NATURAL numbers IN REVERSE using
  228.           (i) simple LOOP
  229.          (ii) WHILE LOOP
  230.          (iii) FOR LOOP
  231.  
  232. DECLARE
  233. a int;
  234. b int;
  235. i int;
  236. c int;
  237. BEGIN
  238. a:=&a;
  239. DBMS_OUTPUT.put_line('Printing using simple loop');
  240. LOOP
  241. EXIT WHEN a<1;
  242. DBMS_OUTPUT.put_line(a);
  243. a:=a-1;
  244. END LOOP;
  245. DBMS_OUTPUT.put_line('Printing using while loop');
  246. c:=&c;
  247. WHILE (c>0) LOOP
  248. DBMS_OUTPUT.put_line(c);
  249. c:=c-1;
  250. END LOOP;
  251. DBMS_OUTPUT.put_line('Printing using for loop');
  252. i:=&i;
  253. FOR b IN REVERSE 1..i LOOP
  254. DBMS_OUTPUT.put_line(b);
  255. END LOOP;
  256. END;
  257. /
  258.  
  259. OUTPUT:
  260. SQL> edit rloop
  261.  
  262. SQL> @rloop
  263. Enter VALUE FOR a: 5
  264. old   7: a:=&a;
  265. NEW   7: a:=5;
  266. Enter VALUE FOR c: 6
  267. old  15: c:=&c;
  268. NEW  15: c:=6;
  269. Enter VALUE FOR i: 8
  270. old  21: i:=&i;
  271. NEW  21: i:=8;
  272. Printing using simple LOOP
  273. 5
  274. 4
  275. 3
  276. 2
  277. 1
  278. Printing using WHILE LOOP
  279. 6
  280. 5
  281. 4
  282. 3
  283. 2
  284. 1
  285. Printing using FOR LOOP
  286. 8
  287. 7
  288. 6
  289. 5
  290. 4
  291. 3
  292. 2
  293. 1
  294.  
  295. PL/SQL PROCEDURE successfully completed.
  296.  
  297. 6. Printing Factorial OF a NUMBER
  298. DECLARE
  299. a int;
  300. f int :=1;
  301. BEGIN
  302. a:=&a;
  303. WHILE a>0 LOOP
  304. f:=f*a;
  305. a:=a-1;
  306. END LOOP;
  307. DBMS_OUTPUT.put_line(f);
  308. END;
  309. /
  310.  
  311. OUTPUT:
  312. SQL> edit fac
  313.  
  314. SQL> @fac
  315. Enter VALUE FOR a: 5
  316. old   5: a:=&a;
  317. NEW   5: a:=5;
  318. 120
  319.  
  320. PL/SQL PROCEDURE successfully completed.
  321.  
  322. 7. Printing Fibonacci series
  323.  
  324. DECLARE
  325. a int:=0;
  326. b int:=1;
  327. n int;
  328. t int;
  329. i int;
  330. BEGIN
  331. n:=&n;
  332. DBMS_OUTPUT.put_line(a);
  333. DBMS_OUTPUT.put_line(b);
  334. FOR i IN 2..n LOOP
  335. t:=a+b;
  336. a:=b;
  337. b:=t;
  338. DBMS_OUTPUT.put_line(t);
  339. END LOOP;
  340. END;
  341. /
  342.  
  343. OUTPUT:
  344. SQL> edit fib
  345.  
  346. SQL> @fib
  347. Enter VALUE FOR n: 9
  348. old   8: n:=&n;
  349. NEW   8: n:=9;
  350. 0
  351. 1
  352. 1
  353. 2
  354. 3
  355. 5
  356. 8
  357. 13
  358. 21
  359. 34
  360.  
  361. PL/SQL PROCEDURE successfully completed.
  362.  
  363. 8.Printing SUM OF Digits (Eg: I/P 425 O/P 4+2+5=11)
  364.  
  365. DECLARE
  366. a int;
  367. b int;
  368. s int;
  369. BEGIN
  370. a:=&a;
  371. s:=0;
  372. WHILE a > 0 LOOP
  373. b:=MOD(a,10);
  374. s:=s+b;
  375. a:=FLOOR(a/10);
  376. END LOOP;
  377. DBMS_OUTPUT.put_line('The sum of digits is: '||s);
  378. END;
  379. /
  380.  
  381. OUTPUT:
  382. SQL> edit sdigit
  383.  
  384. SQL> @sdigit
  385. Enter VALUE FOR a: 234
  386. old   6: a:=&a;
  387. NEW   6: a:=234;
  388. The SUM OF digits IS: 9
  389.  
  390. PL/SQL PROCEDURE successfully completed.
  391.  
  392. 9. Printing Palindrome.
  393.  
  394. DECLARE
  395. a VARCHAR2(20):='naman';
  396. b VARCHAR2(20);
  397. c VARCHAR2(20);
  398. BEGIN
  399. FOR i IN REVERSE 1..LENGTH(a) LOOP
  400. b:=SUBSTR(a,i,1);
  401. c:=c||''||b;
  402. END LOOP;
  403. IF c = a
  404. THEN
  405. DBMS_OUTPUT.put_line(c||' is palndrome');
  406. ELSE
  407. DBMS_OUTPUT.put_line(c||' is not palndrome');
  408. END IF;
  409. END;
  410. /
  411.  
  412. OUTPUT:
  413.  
  414. SQL> edit plan
  415.  
  416. SQL> @plan
  417. naman IS palndrome
  418.  
  419. PL/SQL PROCEDURE successfully completed.
  420.  
  421. 10. Adding only the odd numbers FROM USER input using LOOP. (Eg: I/P 4,8,7,2,3,9,6   O/P 7+3+9=19)
  422.  
  423. DECLARE
  424.   input_string VARCHAR2(100) := '4,5,6,7';
  425.   total_odd_sum NUMBER := 0;
  426.   num VARCHAR2(10);
  427. BEGIN
  428.   LOOP
  429.     num := SUBSTR(input_string, 1, INSTR(input_string, ',') - 1);
  430.     input_string := SUBSTR(input_string, INSTR(input_string, ',') + 1);
  431.     IF MOD(TO_NUMBER(num), 2) = 1 THEN
  432.       total_odd_sum := total_odd_sum + TO_NUMBER(num);
  433.     END IF;
  434.     EXIT WHEN INSTR(input_string, ',') = 0;
  435.   END LOOP;
  436.   IF MOD(TO_NUMBER(input_string), 2) = 1 THEN
  437.     total_odd_sum := total_odd_sum + TO_NUMBER(input_string);
  438.   END IF;
  439.  
  440.   DBMS_OUTPUT.PUT_LINE('Sum of odd numbers: ' || total_odd_sum);
  441. END;
  442. /
  443.  
  444. OUTPUT:
  445.  
  446. SQL> edit osum
  447.  
  448. SQL> @osum
  449. SUM OF odd numbers: 12
  450.  
  451. PL/SQL PROCEDURE successfully completed.
  452. DBMS Lab 9.txt
  453. Displaying DBMS Lab 9.txt.
  454. Lab 9
  455. Gayathry S Warrier Computer Science (Yeshwanthpur)
  456. Oct 7 (Edited Oct 7)
  457. 100 points
  458. Due Oct 7
  459. Dear students
  460. Complete the following list OF simple programs IN PL/SQL
  461. 1. Addition OF two numbers
  462. 2. Area OF Circle
  463. 3. Biggest OF three numbers
  464. 4. Printing 1 TO n NATURAL numbers using
  465.          (i) simple LOOP
  466.          (ii) WHILE LOOP
  467.          (iii) FOR LOOP
  468. 5.  Printing 1 TO n NATURAL numbers IN REVERSE using
  469.           (i) simple LOOP
  470.          (ii) WHILE LOOP
  471.          (iii) FOR LOOP
  472. 6. Printing Factorial OF a NUMBER
  473. 7. Printing Fibonacci series
  474. 8.Printing SUM OF Digits (Eg: I/P 425 O/P 4+2+5=11)
  475. 9. Printing Palindrome.
  476. 10. Adding only the odd numbers FROM USER input using LOOP. (Eg: I/P 4,8,7,2,3,9,6   O/P 7+3+9=19)
  477. Class comments
  478.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement