Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

sum of squares of 1st n natural numbers using PL/SQL

kr61665May 3 2011 — edited May 3 2011
Hi,

I have tried the below procedure but getting individual results only.I want to add all the results i.e (1+4+9).Can anyone please tell me how to query it.

create or replace procedure test_sum_sqrs
as
begin
declare
n number;
s number :=1;
begin
n :=&n;
for i in 1..n
loop
s :=(i*1)*(i*1);
DBMS_OUTPUT.PUT_LINE('Individual values'||' '||s);
end loop;
DBMS_OUTPUT.PUT_LINE('Output is'||' '||s);
end;
end test_sum_sqrs;

Procedure created.

SQL> exec test_sum_sqrs;
Individual values 1
Individual values 4
Individual values 9
Output is 9
This post has been answered by GokhanAtil on May 3 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 31 2011
Added on May 3 2011
6 comments
5,184 views