Monday, February 29, 2016

Sum of Odd Numbers using for loop


Sum of Odd Numbers using for loop


 1  declare
 2     n number;
 3     sum1 number default 0;
 4     endvalue number;
 5  begin
 6     endvalue:=&endvalue;
 7     n:=1;
 8     for n in 1..endvalue
 9     loop
10         if mod(n,2)=1
11           then
12           sum1:=sum1+n;
13         end if;
14     end loop;
15    dbms_output.put_line(' sum  =  '||sum1);
16* end;


SQL> /
Enter value for endvalue: 30
old   6: endvalue:=&endvalue;
new   6: endvalue:=30;

sum  =  225

No comments:

Post a Comment