1

So I am fairly new to coding and I am only pulling 0's back in return. I input 10000 5 and 10 as my 3 inputs when running it and I cannot get anything to return from it other then 0 for all three fields. I thought maybe my program wanted a starting point for the variables so I declared all of them as 0 to start and it still did not work.

int AIR, MIR, PMT, IP, PP, ABorrowed, Term;
        AIR = 0;
        MIR = 0;
        PMT = 0;
        IP = 0;
        PP = 0;
        ABorrowed = 0;
        Term = 0;

        Console.WriteLine("Please enter the amount borrowed on your loan ");
        ABorrowed = int.Parse(Console.ReadLine());
        Console.WriteLine("Please enter the interest rate for your loan ");
        AIR = int.Parse(Console.ReadLine());
        Console.WriteLine("Please enter term of your loan in months ");
        Term = int.Parse(Console.ReadLine());
        MIR = AIR / 12;
        PMT = ABorrowed * (MIR/((1-(1/(1+MIR))^Term)));
        IP = ABorrowed * MIR;
        PP = PMT - IP;
        Console.WriteLine("Your total payment for this month is " + PMT);
        Console.WriteLine("Of that payment " + IP + " is interest rate");
        Console.WriteLine("and the Payment Portion is " + PP);
        Console.ReadLine();
j.s.banger
  • 412
  • 1
  • 6
  • 15
Zi0n1
  • 594
  • 2
  • 5
  • 16
  • 1
    It should be noted that you shouldn't parse an integer directory from user input and assign to an integer. If they input `asdf` your program will crash. Instead use `int.TryParse()`. Decimal also has this method available to it. – The Muffin Man Mar 12 '13 at 22:55
  • As JaredPar stated you should use Decimal instead of an Int, if you look at your code your first calculation is AIR/12 now if I read right your AIR is 5? In other words your first formula is 5/12 which when returned as an int will give your MIR a value of 0, thus in all the rest of our formulas you are multiplying by 0, thus you get values of 0. If you change from int to decimal your prob should be solved. :) – Heinrich Mar 12 '13 at 23:07

5 Answers5

3

There are several problems in this code related to your description:

  1. You're using integer division
  2. You're (most likely) using ^ incorrectly

First, integer division returns an integer, meaning:

10 / 3 = 3

You're much more likely to get correct results if you use decimal instead of int for your types.

Additionally, you're using ^ in there, which I assume is your way of raising something to the power of something else, but ^ is the XOR operator, which does something else entirely.

To raise something to the power of something else in C#/.NET, you use Math.Pow:

PMT = ABorrowed * (MIR/((1-Math.Pow((1/(1+MIR)), Term))));

(I think I managed to place the Math.Pow call around the right parts here)

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I switched it to math.pow and also used doubles...worked right away after a couple of other tweaks which were easy to work out. Thank you so much for your help! – Zi0n1 Mar 12 '13 at 23:01
  • No problem. Remember that you can accept answers here if they answer your question, just check the green checkmark beneath the votes for an answer. And welcome to Stack Overflow, hope you stay. – Lasse V. Karlsen Mar 12 '13 at 23:07
  • 1
    @user2052853: Try to keep financial calculations in *decimal* as much as possible; you'll get fewer weird rounding errors. – Eric Lippert Mar 12 '13 at 23:28
2

The core problem here is that you are using int. This type can only represent whole numbers and is terrible for financial calculations. When doing calculations on money use decimal and not int

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

Use decimal instead of int and ^ isn't the power of it's exclusive or which I doubt is what you've going for. Instead use Math.Pow

Actually looking at the code you could avoid a few of your troubles using var and not declaring everything at the start.

    Console.WriteLine("Please enter the amount borrowed on your loan ");
    var ABorrowed = decimal.Parse(Console.ReadLine());
    Console.WriteLine("Please enter the interest rate for your loan ");

    var AIR = decimal.Parse(Console.ReadLine());
    Console.WriteLine("Please enter term of your loan in months ");

    var Term = decimal.Parse(Console.ReadLine());
    var MIR = AIR / 12;
    var PMT = ABorrowed * (MIR/((1-Math.Pow((1/(1+MIR)), Term))));
    var IP = ABorrowed * MIR;
    var PP = PMT - IP;

    Console.WriteLine("Your total payment for this month is " + PMT);
    Console.WriteLine("Of that payment " + IP + " is interest rate");
    Console.WriteLine("and the Payment Portion is " + PP);
    Console.ReadLine();
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • "For money, always decimal. It's why it was created." http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when – The Muffin Man Mar 12 '13 at 22:59
0

The division operator '/' truncates to the nearest integer closer to zero, so the assignment to MIR clobbers everything.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
0

You have declared your variables as INT, So if you enter 5 for AIR then MIR = AIR / 12 will be zero because 5/12 gets converted to zero. And therefore PMT and IP will be zero and PP will be zero.

You should use decimal, not int for your variable types.

DeanOC
  • 7,142
  • 6
  • 42
  • 56