pl/sql - equation [message #19955] |
Sat, 20 April 2002 10:31 |
Mark
Messages: 284 Registered: July 1998
|
Senior Member |
|
|
Hello, I'm trying to sum all the even numbers from 1 to 1000. i.e. 2+4+6+8+10 =30..I accumulate but when the loop goes to the top, the variable is cleared and the last loop value is displayed. The code below is what I'm using. Can I get a variable to hold the accumulated value so that at the end of the loop it display the total sum of all the looped values? Thanks for the help.
DECLARE
TOTAL NUMBER;
TTOTAL NUMBER;
BEGIN
FOR I IN 1..1000 LOOP
IF mod(I,2)=0 then
total:=I + I;
ttotal:=total + I;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total sum of even numbers: '||ttotal);
END;
/
|
|
|
Re: pl/sql - equation [message #19974 is a reply to message #19955] |
Mon, 22 April 2002 08:24 |
almap
Messages: 3 Registered: April 2002
|
Junior Member |
|
|
try this
DECLARE
TOTAL NUMBER;
TTOTAL NUMBER;
BEGIN
FOR I IN 1..1000 LOOP
IF mod(I,2)=0 then
--total:=I + I;
ttotal:=ttotal + I;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total sum of even numbers: '||ttotal);
END;
|
|
|