Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Getting a different output in my program, but it is supposed to be something different?

$
0
0
Here is the code I am working on
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            /*User will first input the information:
             * (loan amount, annual interest rate, loan period in years and number of payments made each year) 
             * needed for the calculations for the output of the loan summary*/

           
            Console.WriteLine("Please enter your loan amount");
            int _loanAmount = int.Parse(Console.ReadLine());

            Console.WriteLine("Please enter your interest rate");
            decimal _interestRate = Convert.ToDecimal(Console.ReadLine());

            Console.WriteLine("Please enter the loan period in years");
            int _loanPeriod = int.Parse(Console.ReadLine());

            Console.WriteLine("Please enter the number of payments per year");
            int _NumberofMonthlyPayments = int.Parse(Console.ReadLine());            

            // You have all the info from the user, now call the methods passing parameters
            var _amortizationTerm = AmortizationTerm(_loanPeriod, _NumberofMonthlyPayments);
            var _monthlyPayment = MonthlyPayment(_interestRate, _amortizationTerm, _loanAmount);
            var _totalInterest = TotalInterest(_loanAmount, _loanPeriod, _NumberofMonthlyPayments, _monthlyPayment);
            Console.ReadLine();
        }

        /*The information below is needed to calculate the:
         *(the total interest, scheduled payments and number of monthly payments)
           needed for the output of the loan summary*/
        
        public static double MonthlyPayment(decimal _interestRate, double _amortizationTerm, int _loanAmount)
        {
            /*Calculate monthly payment and round to 2 decimal places*/
            var monthlyPayment = (((double)_interestRate / 12) / (1 - (Math.Pow((1 + ((double)_interestRate / 12)), -(_amortizationTerm))))) * _loanAmount;
            monthlyPayment = Math.Round(monthlyPayment, 2);
            return monthlyPayment;
        }

        public static double TotalInterest(int _loanAmount, int _loanPeriod, int _NumberofMonthlyPayments, double _monthlyPayment)
        {
            var _amortizationTerm = AmortizationTerm(_loanPeriod, _NumberofMonthlyPayments);
            var TotalInterest = _monthlyPayment - (_loanAmount / _amortizationTerm) * _amortizationTerm;
            Console.WriteLine("The total interest paid on loan is ${0}", TotalInterest);
            return TotalInterest;
        }


        public static double AmortizationTerm(int _loanPeriod, int _NumberofMonthlyPayments)
        {
            /* Below the following equations represnts the:
             * number of payments (12) multiplied by the loan period in years (30) 
             * to calculate the scheduled and actual number of payments (360) */

            var _amortizationTerm = _loanPeriod * _NumberofMonthlyPayments;
            Console.WriteLine("The total number of monthly scheduled payments is {0}", _amortizationTerm);
            return _amortizationTerm;
        }



        }
    }

And my output is wrong, I am getting a negative number for my interest rate and I am getting two readlines in my console box that tell me what my scheduled monthly payments are

Viewing all articles
Browse latest Browse all 31927

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>