Programming Help

This is a discussion on Programming Help within the C Programming forums, part of the General Programming Boards category; Hello, I have to program a mortgage calculator based on the equal monthly payments formula. I have everything set except ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Programming Help

    Hello,
    I have to program a mortgage calculator based on the equal monthly payments formula. I have everything set except i keep getting 1.#INF as my value. I know this means infinity but can anyone tell me why?

    thanks!
    here is the code:

    #include<stdio.h>
    #include<conio.h>
    #include<math.h>

    main ()
    {
    double price_of_the_house, amount_financed, annual_rate, monthly_pay, total_pay, r;
    int n;

    printf("Enter the price of the house (in $): ");
    scanf("%lf", &price_of_the_house);
    printf("Enter the amount to be financed (in $): ");
    scanf("%lf", &amount_financed);
    printf("Enter the annual percentage rate (in %%): ");
    scanf("%lf", &annual_rate);
    printf("Enter the term in number of months: ");
    scanf("%lf", &n);

    r = annual_rate/12;
    monthly_pay = amount_financed * r * pow(1+r,n)/(pow(1+r,n) - 1);
    total_pay = monthly_pay * 12;

    if ( price_of_the_house < 0 || amount_financed < 0 || annual_rate <= 0 || n < 0
    || amount_financed > 0.8 * price_of_the_house || n % 12 != 0 || n > 480 )
    {
    printf("One of the following errors have occurred:\n");
    printf("1) Price of house, amount financed, annual rate or number of months is/are negative.\n");
    printf("2) Annual rate or number of months is/are zero.\n");
    printf("3) Amount financed is larger than 80%% of the price of the house.\n");
    printf("4) The number of months is not a multiple of 12.\n");
    printf("5) The number of months is greater than 480.\n");
    }
    else
    {
    printf("The price of the house is: %.2f $\n", price_of_the_house);
    printf("The amount to the financed is: %.2f $\n", amount_financed);
    printf("The annual percentage rate is: %.2f %%\n", annual_rate);
    printf("Your monthly payment is: %f $\n", monthly_pay);
    printf("Your total payment will be: %f $\n", total_pay);
    }

    getch(); return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    You're probably dividing by zero. This line looks like a prime candidate for said operation:

    Code:
    monthly_pay = amount_financed * r * pow(1+r,n)/(pow(1+r,n) - 1);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    What's with the rash of posts without code tags!?

    Just how big a font does "READ THIS FIRST" need to be in before people start reading it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Salem View Post
    What's with the rash of posts without code tags!?

    Just how big a font does "READ THIS FIRST" need to be in before people start reading it.
    I'll bet you that if you make it so big it takes up the entire screen, people that post without code tags will still scroll past it, irregardless of its size.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    I wonder if the OP saw the popup which detects untagged code.

    I further wonder whether they 'fixed' it by turning off JS rather than paying attention to the message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21