Need this in C#.
Revisit the program you developed for Problem 1 of Assignment 2. Now, you need to satisfy the following additional requirement. If the user enters a number out of range (e.g., non-positive value), the program displays a warning message about the invalid. The program keeps displaying the warning message until the user enters valid inputs. Use loop statement(s).
Here is my code for Problem1 of Assignment 2:
using System;
namespace Assignment2Problem1
{
class Program
{
static void Main(string[] args)
{
double cashFlow;
double loanAmount;
double loanPeriod;
double interestRate;
double monthlyPayment;
string input;
Console.WriteLine("Please enter your cash flow amount.");
input= Console.ReadLine();
cashFlow = double.Parse(input);
Console.WriteLine("Please enter your loan amount.");
input = Console.ReadLine();
loanAmount = double.Parse(input);
Console.WriteLine("Please enter the length of the loan
period.");
input = Console.ReadLine();
loanPeriod = double.Parse(input);
Console.WriteLine("Please enter your interest rate.");
input = Console.ReadLine();
interestRate = double.Parse(input);
monthlyPayment = (loanAmount * interestRate / 100) / (1 - (1) / Math.Pow(1 + interestRate / 100, loanPeriod));
Console.WriteLine("Your monthly interest pay is ${0:F3}", monthlyPayment);
if (monthlyPayment <= 0.5 * cashFlow && cashFlow>
5000)
{
Console.WriteLine("Loan is eligible");
}
else if (monthlyPayment <= 0.5 * cashFlow || cashFlow >
5000)
{
Console.WriteLine("Loan is conditionally eligible");
}
else
{
Console.WriteLine("Loan is not eligible");
}
}
}
}
Here is the solution,
i have used loop for every entry so that the user do not have to start entering from the beginning.
//your code starts here
using System;
namespace Assignment2Problem1
{
class Program
{
static void Main(string[] args)
{
double cashFlow;
double loanAmount;
double loanPeriod;
double interestRate;
double monthlyPayment;
string input;
while (true)
{
Console.WriteLine("Please enter your cash flow amount.");
input = Console.ReadLine();
cashFlow = double.Parse(input);
// while loop will run until user enters a positive number...
if (cashFlow <= 0)
{
// display error message and continue with the loop
Console.WriteLine("Invalid value entered... please try
again.");
// to continue
continue;
}
// if users enters a valid number then break the loop
else break;
}
while (true)
{
Console.WriteLine("Please enter your loan amount.");
input = Console.ReadLine();
loanAmount = double.Parse(input);
// while loop will run until user enters a positive number...
if (loanAmount <= 0)
{
// display error message and continue with the loop
Console.WriteLine("Invalid value entered... please try
again.");
// to continue
continue;
}
// if users enters a valid number then break the loop
else break;
}
while (true)
{
Console.WriteLine("Please enter the length of the loan
period.");
input = Console.ReadLine();
loanPeriod = double.Parse(input);
// while loop will run until user enters a positive number...
if (loanPeriod <= 0)
{
// display error message and continue with the loop
Console.WriteLine("Invalid value entered... please try
again.");
// to continue
continue;
}
// if users enters a valid number then break the loop
else break;
}
while (true)
{
Console.WriteLine("Please enter your interest rate.");
input = Console.ReadLine();
interestRate = double.Parse(input);
// while loop will run until user enters a positive number...
if (interestRate <= 0)
{
// display error message and continue with the loop
Console.WriteLine("Invalid value entered... please try
again.");
// to continue
continue;
}
// if users enters a valid number then break the loop
else break;
}
monthlyPayment = (loanAmount * interestRate / 100) / (1 - (1) /
Math.Pow(1 + interestRate / 100, loanPeriod));
Console.WriteLine("Your monthly interest pay is ${0:F3}", monthlyPayment);
if (monthlyPayment <= 0.5 * cashFlow && cashFlow >
5000)
{
Console.WriteLine("Loan is eligible");
}
else if (monthlyPayment <= 0.5 * cashFlow || cashFlow >
5000)
{
Console.WriteLine("Loan is conditionally eligible");
}
else
{
Console.WriteLine("Loan is not eligible");
}
}
}
}
// code ends here
here is the sample ouput:-
Thank You.
Get Answers For Free
Most questions answered within 1 hours.