Home » SQL & PL/SQL » SQL & PL/SQL » PL/SQL
PL/SQL [message #10327] Wed, 14 January 2004 21:41 Go to next message
Thato
Messages: 8
Registered: January 2004
Junior Member
Hi! Guys

 

Its me again, thanx very much for your help last time, but I need more help now, with the following queries:

1. Write a PL/SQL block, which computes the area of a circle.

Hint: Use the exponentiation operator. The area of a circle is 22/7*(radius)squared.

2. Write a PL/SQL block that computes the circumference of a circle. Hint: The circumference of a circle is 2* 22/7 * radius.

3. Write a PL/SQL block that accepts a year from the user and determines if it is a leap year. Hint: If a year is divisible by 4 but not by 100 OR if the year is divisible by 400, it is a leap year.

Please help guys, I'm still learning.
Re: PL/SQL [message #10330 is a reply to message #10327] Wed, 14 January 2004 23:30 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Ok class, here we go:
1. The exponentiation operator in Oracle is "**". In a quick and dirty PL/SQL block, this gives:
SQL> Declare
  2    v_in Number := &radius;
  3  Begin
  4    Dbms_Output.Put_Line( 'The area of a circle with a radius of '||v_in
  5                        ||' is approximately '||(22/7)*(v_in**2)
  6                        );
  7  End;
  8  /
Enter value for radius: 2
old   2:   v_in Number := &radius;
new   2:   v_in Number := 2;
The area of a circle with a radius of 2 is approximately
12,57142857142857142857142857142857142856

PL/SQL procedure successfully completed.
Additional assigment: make a function out of the given anonymous block.

2. You can easily add this to the preceding block:
SQL> Declare
  2    v_in Number := &radius;
  3  Begin
  4    Dbms_Output.Put_Line( 'The area of a circle with a radius of '||v_in
  5                        ||' is approximately '||(22/7)*(v_in**2)
  6                        );
  7    Dbms_Output.Put_Line( 'The circumference of a circle with a radius of '||v_in
  8                        ||' is approximately '||2;*(22/7)*v_in
  9                        );
 10  End;
 11  /
Enter value for radius: 5
old   2:   v_in Number := &radius;
new   2:   v_in Number := 5;
The area of a circle with a radius of 5 is approximately
78,5714285714285714285714285714285714285
The circumference of a circle with a radius of 5 is approximately
31,4285714285714285714285714285714285714

PL/SQL procedure successfully completed.
There's, aside from the calculation used, no real difference between 1 and 2 so here's the same additional assignment: transform into a function that accepts a radius and returns the circumference.

3. Hint: use MOD (modulo division).
SQL> Declare
  2    v_year number := &year;
  3  Begin
  4    If ( Mod(v_year, 4) = 0 And Mod(v_year, 100) != 0 ) Or Mod(v_year, 400) = 0
  5    Then
  6      Dbms_Output.Put_Line('Leap Year');
  7    Else
  8      Dbms_Output.Put_Line('Not a leap year');
  9    End If;
 10  End;
 11  /
Enter value for year: 1982
old   2:   v_year number := &year;
new   2:   v_year number := 1982;
Not a leap year

PL/SQL procedure successfully completed.

SQL> /
Enter value for year: 1988
old   2:   v_year number := &year;
new   2:   v_year number := 1988;
Leap Year

PL/SQL procedure successfully completed.

SQL> /
Enter value for year: 2000
old   2:   v_year number := &year;
new   2:   v_year number := 2000;
Leap Year

PL/SQL procedure successfully completed.

SQL> 
You can test this without calculations, just assign 29-FEB of the year specified to date variable. If it fails with ORA-1839 it is not a leap year:
SQL> Declare
  2    v_year number := &year;
  3    v_test_date   Date;
  4  Begin
  5    Select to_date('29-02-'||v_year,'DD-MM-YYYY')
  6      Into v_test_date
  7      From dual;
  8      -- If we get here it means that 29 FEB is valid for the year
  9      Dbms_Output.Put_Line('Leap year');
 10  Exception
 11    When Others Then
 12      If sqlcode = -1839 -- date not valid for the year specified, so not a leap year
 13      Then
 14        dbms_output.put_line('Not a Leap Year');
 15      Else -- other error
 16        dbms_output.put_line('ERROR: '||SQLERRM);
 17      end if;
 18  End;
 19  /
Enter value for year: 2000
old   2:   v_year number := &year;
new   2:   v_year number := 2000;
Leap year

PL/SQL procedure successfully completed.

SQL> /
Enter value for year: 1988
old   2:   v_year number := &year;
new   2:   v_year number := 1988;
Leap year

PL/SQL procedure successfully completed.

SQL> /
Enter value for year: 1900
old   2:   v_year number := &year;
new   2:   v_year number := 1900;
Not a Leap Year

PL/SQL procedure successfully completed.
Additional assignment:
make a function out of either version, that returns 1 if it's a leap year and 0 if not.

That's all for today class.

MHE
Previous Topic: Is numeric ?
Next Topic: Commit within a select statement
Goto Forum:
  


Current Time: Fri Mar 29 10:39:44 CDT 2024