Need help in C#

charlieharper

Expert Member
Joined
Jun 1, 2007
Messages
3,862
Reaction score
1,977
Location
South Coast, KZN
I'm quite new to the programming world, but after doing tons of research, I thought of learning C#.

I wrote (atleast trying :p to) a simple Command prompt calculator that can automatically calculate Compunded Rent, with the Formula A=P(1+I)^N .

This is my Code so far, although I dont really get what Error 'P' is a 'variable' but is used like a 'method' means :o:o

Code:
//namespace Interest
using System;

public class Interest
{
    public static void Main(string[] args)
    {
        int A; //Amount
        int P; //Price
        int I; //Interest
        int N; //Time

        Console.Write("Price of the Purchase:    ");
        //P
        P = Convert.ToInt32(Console.ReadLine());

        Console.Write("Current Interest Rate:   ");
        //I
        I = Convert.ToInt32(Console.ReadLine());

        Console.Write("Installments in Years:     ");
        //n
        N = Convert.ToInt32(Console.ReadLine());

        A = P *(1 + I)^N; //Formula
        Console.WriteLine("Amount Is: {0}", A =P * (1 + I) ^ N); //Formula
       
        Console.Write("Did this Work?   ");
        A = Convert.ToInt32(Console.ReadLine());
    }
}


Thanks
 
I ran your program and it works. Not sure where you are hitting that error.

Something to remember:

Always make sure your code is 'safe'. It is really easy to crash the program by entering a letter instead of an integer etc. Go and read about Try Catch statements which will help in this regard. It's also good practise to give your variables initial default values when you declare them. I.e.: int A = 0; Perhaps use doubles instead of ints in this scenario.
One more thing - make your variable names more descriptive. Theres nothing wrong with using 'amount' instead of 'A'.
 
shouldn't it be
Code:
        public static void Main(string[] args)
        {
            int Amount; 
            int Price; 
            int Interest; 
            int Time;       

            //Price
            Console.Write("Price of the Purchase:    ");
            Price = Convert.ToInt32(Console.ReadLine());

            //Interest
            Console.Write("Current Interest Rate:   ");
            Interest = Convert.ToInt32(Console.ReadLine());

            //Time
            Console.Write("Installments in Years:     ");
            Time = Convert.ToInt32(Console.ReadLine());

            //Formula
            Amount = Price * (1 + Interest) ^ Time;
            Console.WriteLine("Amount Is: {0}", Amount);

            Console.Write("Did this Work?   ");
[COLOR="Red"]            //Amount = Convert.ToInt32(Console.ReadLine());[/COLOR]
        }

what are you trying to read at the end ?, surely its not amount again ?

as hyperian said, don't just assume you are getting correct values from user- do a test first before asignment. doubles would be better too...
 
Last edited:
Actually Decimal would be the best choice for Compounded Interest unlike double, decimal stores floating point numbers in base 10, using base 2 leads to inaccuracy.

I can't recall the specifics too well because I haven't dealt with that stuff in over 3 years but it's based on the floating point implementation used.
 
I was thinking of being able to add decimals, but have No Idea how to program it. :confused::confused::p

Code:
            bool end = true;
            decimal k = 0;
            while (end)
            {
                try
                {
                    Console.Write("Enter a decimal number:");
                    k = Convert.ToDecimal(Console.ReadLine());
                    end = false;
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a valid decimal number");
                }
                catch (OverflowException)
                {
                    Console.WriteLine("The value is too large to be stored as decimal.");
                }
            }

This code reads a decimal number from the console and stores it as variable k.

It will also show a error message if the user doesn't enter a valid decimal number.
 
Basically adding what I did before to your program:

Code:
using System;

public class Interest
{
    public static decimal FriendlyGetDecimal(string message)
    {
        bool end = true;
        decimal k = 0;
        while (end)
        {
            try
            {
                Console.Write("Enter a " + message+ ": ");
                k = Convert.ToDecimal(Console.ReadLine());
                end = false;
            }
            catch (FormatException)
            {
                Console.WriteLine("Please enter a valid decimal number");
            }
            catch (OverflowException)
            {
                Console.WriteLine("The value is too large to be stored as decimal.");
            }
        }
        return k;
    }

    public static void Main(string[] args)
    {
        decimal A; //Amount
        decimal P; //Price
        decimal I; //Interest
        decimal N; //Time

        P = FriendlyGetDecimal("Price of the Purchase");

        //I
        I = FriendlyGetDecimal("Current Interest Rate");

        //n
        N = FriendlyGetDecimal("Installments in Years");

        A = (decimal) Math.Pow(((double)(P * (1 + I))), ((double)N)); //Formula

        Console.WriteLine("Amount Is: {0}", A ); //Formula

        Console.Write("Did this Work?   ");
        Console.ReadKey();
    }
}

Forget what I wrote above tho, it is a hack because I cast to and from double.

What that means is not important what is important is this part of your code:

P *(1 + I)^N;

It won't work as expected.

The ^ operator is bitwise exclusive OR or XOR. It's not the same as Math.Pow which raises a operand A to the power of B. IE. double k = Math.Pow(a,b);

To illustrate the point, run the following:

Code:
using System;

public class XORTest
{

    public static void Main(string[] args)
    {
        int a = 1;
        int b = 2;
        Console.WriteLine(a ^ b);
        Console.ReadKey();
    }
}

Not what you would expect ;)
 
Last edited:
About the maths...

You are calculating compound interest.... What will the FutureValue(A) off my Investment(P) be after ? years (T), at ? interest(I). Interest must be a fraction ( I / 100 ).
Do a simple test....
1000 after 1 year at 10% : 1000 + 100 = 1100
1000 after 2 years at 10% : 1100 + 110 = 1210.
1000 after 3 years at 10% : 1210 + 121 = 11331.
.... and so on.

Your formula allows for the interest to be calculated only once a year. In the real world, it is usually calculated monthly (12 times a year).
The formula will then look like this: A = P * ( 1 + ( I / 12 ) ) ^ ( T * 12 )
Testing again (calculated monthly)
1000 after 1 month at 10% : 1000 + ( 1000 * ( 0.10 / 12 ) ) = 1008.33
1000 after 2 months at 10% : 1008.33 + ( 1008.33 * ( 0.10 / 12 ) ) = 1016.73
1000 after 3 months at 10% : 1016.73 + ( 1016.73 * ( 0.10 / 12 ) ) = 1025.20
.
.
.
1000 after 12 months ( 1 year ) at 10% : 1095.83 + (1095.83 * ( 0.10 / 12 ) ) = 1104.73



Code:
        static void Main(string[] args)
        {
            double A; //Future Value
            double P; //Initial Investment
            double I; //Interest
            double T; //Term in Years

            Console.Write("Initial Investment:    ");
            P = Convert.ToDouble(Console.ReadLine());

            Console.Write("Current Interest Rate:   ");
            I = Convert.ToDouble(Console.ReadLine())[COLOR="#ff0000"] / 100[/COLOR];  
            
            Console.Write("Term in Years:     ");
            T = Convert.ToDouble(Console.ReadLine());
            
            A = P *[COLOR="#ff0000"] Math.Pow[/COLOR]((1 + (I [COLOR="#ff0000"]/ 12[/COLOR])), T [COLOR="#ff0000"]* 12[/COLOR]);   
            Console.WriteLine("The value of your Investment at the end of the Term is: {0}", A);

            Console.WriteLine("Did this Work?   ");
            Console.ReadLine();
        }
 
Last edited:
I found the formula for RePayments on a Loan in my old Delphi App.

Here it is... I gave it a quick test and it seems to be ok.

Code:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double MP; //Monthly Payments - The value you want to calculate
            double LA; //Loan Amount
            double Term; //Payment Term in Years
            double Rate; //Interest Rate

            Console.Write("Loan Amount:    ");
            LA = Convert.ToDouble(Console.ReadLine());

            Console.Write("Inerest Rate :    ");
            Rate = Convert.ToDouble(Console.ReadLine());

            Console.Write("Repayment Term in Years :    ");
            Term = Convert.ToDouble(Console.ReadLine());

            Rate = Rate / 12 / 100;  // Monthly Calculated in a fraction

            MP = LA / ((1 - Math.Pow(1 + Rate, -Term * 12)) / Rate );  //How is that ????

            Console.WriteLine("Monthly Payment on your loan is : {0}", MP);

            Console.WriteLine("Press 'Enter' to exit");
            Console.ReadLine();
        }
    }
}
 
Top
Sign up to the MyBroadband newsletter
X