Home » SQL & PL/SQL » SQL & PL/SQL » display of sum 1 to 100
display of sum 1 to 100 [message #197425] Wed, 11 October 2006 04:23 Go to next message
arunprabhu29917
Messages: 15
Registered: September 2006
Location: Tamilnadu
Junior Member

1 declare
2 v_no number :=100;
3 s number:=0;
4 begin
5 for i in 1.. v_no
6 loop
7 s:=s+i;
8 end loop;
9 dbms_output.put_line('sum is:');
10* end;
QL> /

pL/SQL procedure successfully completed.


but i am not geeting answer....in the screen?

[Updated on: Wed, 11 October 2006 04:33]

Report message to a moderator

Re: display of sum 1 to 100 [message #197426 is a reply to message #197425] Wed, 11 October 2006 04:26 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Issue this command in SQL*Plus before running the script
SET SERVEROUTPUT ON
Re: display of sum 1 to 100 [message #197429 is a reply to message #197425] Wed, 11 October 2006 04:32 Go to previous messageGo to next message
ebrian
Messages: 2794
Registered: April 2006
Senior Member
You could acutally just perform it in SQL:

SQL> select sum(level) from dual
  2	connect by level <= 100;

SUM(LEVEL)
----------
      5050
Re: display of sum 1 to 100 [message #197435 is a reply to message #197429] Wed, 11 October 2006 04:42 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Good point.
Ah!!!! A chance to use all that Maths I learned.

You could just write
SELECT n*(n+1)/2 FROM dual;
Re: display of sum 1 to 100 [message #197450 is a reply to message #197435] Wed, 11 October 2006 05:21 Go to previous messageGo to next message
ebrian
Messages: 2794
Registered: April 2006
Senior Member
JRowbottom wrote on Wed, 11 October 2006 04:42

SELECT n*(n+1)/2 FROM dual;


The ol' Gauss formula....definitely the way to go !!!
Re: display of sum 1 to 100 [message #197456 is a reply to message #197425] Wed, 11 October 2006 05:27 Go to previous message
arunprabhu29917
Messages: 15
Registered: September 2006
Location: Tamilnadu
Junior Member

hi to all,


thank you for all prompt responce.

[Updated on: Wed, 11 October 2006 05:27]

Report message to a moderator

Previous Topic: trigger and function
Next Topic: Need help for a query.
Goto Forum:
  


Current Time: Wed Dec 07 10:43:08 CST 2016

Total time taken to generate the page: 0.05888 seconds