CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011

    Unhappy Having trouble with compound interest program

    namespace ConsoleApplication1
    class Program
    static void Main(string[] args)
    //prompt user to enter principal
    Console.WriteLine( "enter principle:");
    string principalInput = Console.ReadLine();
    double principal = Convert.ToDouble(principalInput);
    //make sure princiapl is not negative
    if (principal < 0)
    Console.WriteLine("Principal cannot be negative");
    principal = 0;
    //enter intrest rate
    Console.WriteLine("enter intrest rate");
    string intrestInput = Console.ReadLine() ;
    double intrest = Convert.ToDouble(intrestInput);
    //make sure intrest is not negative.
    if (intrest < 0)
    Console.WriteLine("intrest cannot be negative");
    intrest = 0;
    //calculate the value of principal plus intrest.
    double intrestPaid= principal*(intrest/100);
    //now calculate the total
    double total = principal + intrestPaid;
    // output the result
    Console.WriteLine();//skip a line
    Console.WriteLine("principal =" +principal);
    Console.WriteLine("Intrest ="+ intrest + "%");
    Console.WriteLine("intrest paid =" +intrestPaid);
    Console.WriteLine("Total =" +total);
    Console.WriteLine("If you want compound intrest enter the number of years,if not close now");
    string years1=Console.ReadLine();
    double years2 = Convert.ToDouble(years1);
    double CompoundIntrest = principal * ((double)Math.Pow(1.0+intrest, years2));
    Console.WriteLine("Compound intrest is" +CompoundIntrest);
    //Wait for response then tell the user how to leave
    Console.WriteLine ("Press Enter to Terminate...");
    Console .Read() ;
    I beleive the error here is that i have used Math.Pow which is what my friend has told me to use
    the purpose i to figure out a compound intrest so if the princaple is 100 and teh intrest 10 and years is 2 i should get 121
    can somebody rewrite this for me and tell me what would work
    thank you forums

  2. #2
    Join Date
    Feb 2011
    United States

    Re: Having trouble with compound interest program

    You are very close and even have identified the correct line that is in error.

    double CompoundIntrest = principal * ((double)Math.Pow(1.0+intrest, years2));
    Look carefully at the value stored in intrest (hint: it shouldn't be > 1). To compound at, say, 30 percent interest you would perform the following calculation:

    Principle * (1.3)^(years)

    Does that make it clear now you need to modify the program? Try some things and if you still have problems let us know.

    Also, this is unrelated: Also Math.Pow returns a double, so you don't need to use a cast.
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.