Friday, March 4, 2016

Sum of Odd Numbers using for loop

Sum of Odd Numbers using for loop


 declare
 n number;
 sum1 number default 0;
 endvalue number;
 begin
 endvalue:=&endvalue;
 n:=1;
 for n in 1..endvalue
 loop
if mod(n,2)=1
then
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line(' sum  =  '||sum1);
end;

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

sum  =  225

No comments:

Post a Comment