For.....loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deven Oza
    New Member
    • Oct 2006
    • 53

    For.....loop

    Can anybody solve the query for me please

    In June of 2005, Sammy puts $ 1,024 into a savings account and, each month thereafter, plans to deposit only half of what he saved in the previous month. Using a single for loop, write a program that will show that he will have deposited $ 2,047.50 into his account by the end of May 2006. Express your output as a dollar amount, with two decimal places.

    Thanks
    Dev
  • sushma
    New Member
    • Dec 2006
    • 4

    #2
    Originally posted by Deven Oza
    Can anybody solve the query for me please

    In June of 2005, Sammy puts $ 1,024 into a savings account and, each month thereafter, plans to deposit only half of what he saved in the previous month. Using a single for loop, write a program that will show that he will have deposited $ 2,047.50 into his account by the end of May 2006. Express your output as a dollar amount, with two decimal places.

    Thanks
    Dev


    hi the solution for your problem is:

    SQL> declare
    2 present number(7,2):=10 24.00;
    3 inc number(10,2);
    4 final number(10,2);
    5 mon number;
    6 begin
    7 final:=1024.00;
    8 mon := months_between( to_date('may-06','mon-yy'),to_date('j une-05','mon-yy'));
    9 for i in 1..mon loop
    10 present :=present/2;
    11 final := final+present;
    12 dbms_output.put _line('savings in the month of '||i||'='||pres ent);
    13 end loop;
    14 dbms_output.put _line('final amount is : '||ltrim(to_cha r(final,'L99G99 9D99MI')));
    15 end;
    16 /
    savings in the month of 1=512
    savings in the month of 2=256
    savings in the month of 3=128
    savings in the month of 4=64
    savings in the month of 5=32
    savings in the month of 6=16
    savings in the month of 7=8
    savings in the month of 8=4
    savings in the month of 9=2
    savings in the month of 10=1
    savings in the month of 11=.5
    final amount is : $2,047.50

    PL/SQL procedure successfully completed.


    hope this helped you. if there are any issues let me know

    Comment

    • pragatiswain
      Recognized Expert New Member
      • Nov 2006
      • 96

      #3
      Originally posted by sushma
      hi the solution for your problem is:

      SQL> declare
      2 present number(7,2):=10 24.00;
      3 inc number(10,2);
      4 final number(10,2);
      5 mon number;
      6 begin
      7 final:=1024.00;
      8 mon := months_between( to_date('may-06','mon-yy'),to_date('j une-05','mon-yy'));
      9 for i in 1..mon loop
      10 present :=present/2;
      11 final := final+present;
      12 dbms_output.put _line('savings in the month of '||i||'='||pres ent);
      13 end loop;
      14 dbms_output.put _line('final amount is : '||ltrim(to_cha r(final,'L99G99 9D99MI')));
      15 end;
      16 /
      savings in the month of 1=512
      savings in the month of 2=256
      savings in the month of 3=128
      savings in the month of 4=64
      savings in the month of 5=32
      savings in the month of 6=16
      savings in the month of 7=8
      savings in the month of 8=4
      savings in the month of 9=2
      savings in the month of 10=1
      savings in the month of 11=.5
      final amount is : $2,047.50

      PL/SQL procedure successfully completed.

      hope this helped you. if there are any issues let me know
      Can try this one too.

      declare
      ibaseAmt number(10,2);
      iTotalAmt number(10,2);
      iMonth Number(2);

      Begin
      ibaseAmt := 1024;
      iMonth := 1;
      iTotalAmt:=ibas eAmt;
      dbms_output.put line('Month:'|| iMonth ||',Deposit Amt For This

      month: $'|| ibaseAmt ||', Total Amount Deposited: $' || iTotalAmt);
      While (iTotalAmt < 2047.50 )

      ibaseAmt := ibaseAmt/2;
      iTotalAmt:= iTotalAmt + ibaseAmt;
      iMonth := iMonth + 1;
      dbms_output.put line('Month:'|| iMonth ||',Deposit Amt For

      This month: $'|| ibaseAmt ||', Total Amount Deposited: $' ||

      iTotalAmt);

      do
      End;

      Comment

      • Deven Oza
        New Member
        • Oct 2006
        • 53

        #4
        Originally posted by sushma
        hi the solution for your problem is:

        SQL> declare
        2 present number(7,2):=10 24.00;
        3 inc number(10,2);
        4 final number(10,2);
        5 mon number;
        6 begin
        7 final:=1024.00;
        8 mon := months_between( to_date('may-06','mon-yy'),to_date('j une-05','mon-yy'));
        9 for i in 1..mon loop
        10 present :=present/2;
        11 final := final+present;
        12 dbms_output.put _line('savings in the month of '||i||'='||pres ent);
        13 end loop;
        14 dbms_output.put _line('final amount is : '||ltrim(to_cha r(final,'L99G99 9D99MI')));
        15 end;
        16 /
        savings in the month of 1=512
        savings in the month of 2=256
        savings in the month of 3=128
        savings in the month of 4=64
        savings in the month of 5=32
        savings in the month of 6=16
        savings in the month of 7=8
        savings in the month of 8=4
        savings in the month of 9=2
        savings in the month of 10=1
        savings in the month of 11=.5
        final amount is : $2,047.50

        PL/SQL procedure successfully completed.


        hope this helped you. if there are any issues let me know

        Hi Shushma,
        it works ,
        Thank you very much
        -Deven

        Comment

        Working...