a=10000;   %   begining balance
i=5;  %   interest rate in %        
N=12;   % payments/y
pay=30;   %  payment

k=0;  tot=0;
while 1
      a = a + a*i/100/N - pay;
      k=k+1;
      balance(k)=a;
      int=a*i/100/N;
      tot=tot+int;
      interest(k)=int;
      fprintf('payment %4d  balance   %10.2f   interest  %10.2f\n',k,a,int);
      if a<0
          a=a+pay;
          pay=a;
          break
      end
      if k>100 break;  end
    
end
fprintf(' number of payments: %d   last payment:  %10.2f',k,pay);
fprintf(' total interest paid:  %10.2f\n',tot);

