DECLARE V_Operator CHAR(1):= '&enterOpearator'; V_Num1 NUMBER := &enterNum1; V_Num2 NUMBER := &enterNum2; BEGIN IF V_Operator = '*' THEN DBMS_OUTPUT.PUT_LINE('Result of '||V_num1||' * '||V_Num2||' = '||(V_Num1*V_Num2)); ELSIF V_Operator = '/' THEN DBMS_OUTPUT.PUT_LINE('Result of '||V_num1||' / '||V_Num2||' = '||(V_Num1/V_Num2)); ELSIF V_Operator = '+' THEN DBMS_OUTPUT.PUT_LINE('Result of '||V_num1||' + '||V_Num2||' = '||(V_Num1+V_Num2)); ELSIF V_Operator = '-' THEN DBMS_OUTPUT.PUT_LINE('Result of '||V_num1||' - '||V_Num2||' = '||(V_Num1-V_Num2)); ELSE DBMS_OUTPUT.PUT_LINE('Invalid Operator'); END IF; END; /
/* Program to swap 2 numbers if first one is greater than the second. Else print the two numbers as usual. Numbers are swapped without third variable. */ DECLARE V_Num1 NUMBER :=&GNum1; --Local variable. V_Num2 NUMBER :=&GNum2; --Local variable. BEGIN DBMS_OUTPUT.PUT_LINE('V_Num1 = '||V_Num1); DBMS_OUTPUT.PUT_LINE('V_Num2 = '||V_Num2); IF V_Num1 > V_Num2 THEN V_Num1 :=V_Num1+V_Num2; V_Num2 :=V_Num1-V_Num2; V_Num1 :=V_Num1-V_Num2; DBMS_OUTPUT.PUT_LINE('Swapped V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Swapped V_Num2 : '||V_Num2); ELSE DBMS_OUTPUT.PUT_LINE('Original V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Original V_Num2 ; '||V_Num2); END IF; END; /
/* Program to swap 2 numbers if first one is greater than the second. Else print the two numbers as usual. */ DECLARE V_Num1 NUMBER := &GNum1; --Local variable. V_Num2 NUMBER := &GNum2; --Local variable. BEGIN DBMS_OUTPUT.PUT_LINE('V_Num1 = '||V_Num1); DBMS_OUTPUT.PUT_LINE('V_Num2 = '||V_Num2); IF V_Num1 > V_Num2 THEN V_Num1 :=V_Num1+V_Num2; V_Num2 :=V_Num1-V_Num2; V_Num1 :=V_Num1-V_Num2; DBMS_OUTPUT.PUT_LINE('Swapped V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Swapped V_Num2 : '||V_Num2); ELSE DBMS_OUTPUT.PUT_LINE('Original V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Original V_Num2 ; '||V_Num2); END IF; END; /
/* Program that takes two numbers and print their values. Swaps the number if first one is greater than the second one and print their swapped values. Else print their values as it is. Swapping is done by using a temporary variable. */ DECLARE V_Num1 NUMBER := &enterNum1; --First local variable. V_Num2 NUMBER := &enterNum2; --Second local variable. V_Temp NUMBER; --Temporary variable used for swapping. BEGIN DBMS_OUTPUT.PUT_LINE('Inputted value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Inputted value of V_Num2 : '||V_Num2); IF V_Num1 > V_Num2 THEN V_Temp :=V_Num1; V_Num1 :=V_Num2; V_Num2 :=V_temp; DBMS_OUTPUT.PUT_LINE('Swapped value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Swapped value of V_Num2 : '||V_Num2); ELSE DBMS_OUTPUT.PUT_LINE('Original value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Original value of V_num2 : '||V_Num2); END IF; END; /
NULLs are very important consideration in any PL/SQL to make your program bug free. If you provide NULL values then always the ELSE part is executed.
/* The above program handling NULL values. */ DECLARE --First Block Begins. V_Num1 NUMBER := &enterNum1; --First local variable. V_Num2 NUMBER := &enterNum2; --Second local variable. V_Temp NUMBER; --Temporary variable used for swapping. BEGIN DBMS_OUTPUT.PUT_LINE('Inputted value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Inputted value of V_Num2 : '||V_Num2); IF V_Num1 IS NULL OR V_Num2 IS NULL THEN DBMS_OUTPUT.PUT_LINE('Fatal Error! Can''t Proceed. Enter valid numbers.'); ELSE IF V_Num1 > V_Num2 THEN V_Temp :=V_Num1; V_Num1 :=V_Num2; V_Num2 :=V_temp; DBMS_OUTPUT.PUT_LINE('Swapped value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Swapped value of V_Num2 : '||V_Num2); ELSE DBMS_OUTPUT.PUT_LINE('Original value of V_Num1 : '||V_Num1); DBMS_OUTPUT.PUT_LINE('Original value of V_num2 : '||V_Num2); END IF; END IF; END; /
Q : Write a program to find whether a year is leap year or not?
Ans: Let us first understand what a leap year is.
A leap year occurs mostly every 4 years, but every 100 years we skip a leap year unless then number is divisible by 400.
- ✔ Leap years 1600, 2000
- ✘ Leap years 1700, 1800, 1900, 2100
/* Program to find if year entered is a leap year or not. */ DECLARE V_Year NUMBER(4) := &enterYear; BEGIN IF V_Year <= 0 THEN DBMS_OUTPUT.PUT_LINE('Please enter a year greater than 0'); EXIT; END IF; IF MOD(V_Year, 4) =0 THEN IF MOD(V_Year, 100) <>0 THEN DBMS_OUTPUT.PUT_LINE(V_Year||' is a Leap year.'); ELSE IF MOD(V_Year, 400) =0 THEN DBMS_OUTPUT.PUT_LINE(V_Year||' is a Leap year.'); ELSE DBMS_OUTPUT.PUT_LINE(V_Year||' isn''t a Leap year'); END IF; END IF; ELSE DBMS_OUTPUT.PUT_LINE(V_Year||' isn''t a Leap year'); END IF; END; /