1 Reply Latest reply: Oct 8, 2012 8:09 AM by GauravBhide RSS

    Increase in price, yearly.. and calculate the difference between the years.

    963364
      Hi everyone

      I need to make the following code show how many years it took for it to reach its required goal.
      show difference between the years and how much money increased ever year. ..

      I am totally confused at the moment... any help would be greatly appreciated...

      declare
      a number(9):=1000;
      b number(9);
      c number(9);
      d number(9);
      begin

      for d in 2012..2099
      loop
      dbms_output.put_line(a||' '||d);
      if a >2000 then
      exit;
      end if;
      a :=a + a*10/100;
      end loop;
      end;
      /
      Result for above query:
      Price Years
      1000 2012
      1100 2013
      1200 2014 etc...

      I need to show the amount of years it took to reach its goal, which was $2000
      and also the change in price, how much it increased every year...

      Thanks

      Umair
        • 1. Re: Increase in price, yearly.. and calculate the difference between the years.
          GauravBhide
          Hi Umair ,

          I am not sure why you have created so many different variables. ( b , c , d )

          Try this
          declare
              a                number(9)  :=1000;
              cnt              number      := 0;
          
          begin
          
              for d in 2012..2099 loop
          
                  if a > 2000 then     
          
                      exit;
          
                  end if;
          
                  dbms_output.put_line(a||'   '||d);
          
                  a :=a + a*10/100;
          
                  cnt := cnt + 1;
          
              end loop;
          
              dbms_output.put_line( 'Number of years required to reach the goal are:- ' || to_number(cnt) );
          
          end;
          /
          If you want to show number of years in yyyy way ( example :- 2020) then set cnt to 2012 as for loop is starting with 2012.

          Thanks.