Please help, im using visual studio From the main function (inside a loop)., you
ID: 3920612 • Letter: P
Question
Please help, im using visual studio From the main function (inside a loop)., you should first call the function menu. Function menu should display the following legal options Quit Get an exponential or factorial value (Your choice) Get the area of circle (pi 3.1415) Get the real (float) value of two integers The menu function should keep the user in the function until they enter a valid option (i.e., t should check the user's entry, notifying them about an illegal entry. If a legal value is entered should be returned to the main function. If the user wants to quit, the program should be terminated. Otherwise, the program should transfer control to one of the other functions (exponentiation/factorial, area of a circle, real value of two integers). Of the three functions, at least two should return values back to the main function. The main function should again display the menu and get the user's (legal) choice. The above procedure should continue until the user decides to quit.Explanation / Answer
Hi,
The program fulfilling all the requirements is written below. The 3rd function about "Get the real value(float) of two integers" is little unclear. Still i have tried to answer as per my understanding.
If it answers your question clearly, please upvote it. Thanks in advance.
using System;
namespace Chegg_MathFunction
{
class Program
{
const double pi = 3.142;
static void Main(string[] args)
{
Console.WriteLine("Welcome!");
bool shouldContinue = true;
while (shouldContinue)
{
Console.WriteLine("Select any option below(Select corresponding number or Type the option) : ");
Console.WriteLine("1 - Get a factorial value");
Console.WriteLine("2 - Get the area of circle");
Console.WriteLine("3 - Get real value of two integers");
Console.WriteLine("4 - Quit");
string option = Console.ReadLine().ToLower();
switch (option)
{
case "1":
case "get a factorial value":
int factorial = DoFactorial();
Console.WriteLine("The factorial is {0}", factorial);
shouldContinue = true;
break;
case "2":
case "get the area of circle":
double area = DoCircleArea();
Console.WriteLine("Area of circle is {0}", area);
shouldContinue = true;
break;
case "3":
case "get real value of two integers":
float realValue2;
float realValue1 = DoRealConversion(out realValue2);
Console.WriteLine(string.Format("Real values are {0:0.0} and {1:0.0}", realValue1, realValue2));
shouldContinue = true;
break;
case "4":
case "quit":
shouldContinue = false;
break;
default:
Console.WriteLine("Invalid option!");
shouldContinue = true;
break;
}
}
}
private static int DoFactorial()
{
Console.WriteLine("Please enter a number : ");
string input = Console.ReadLine();
int inputNumber;
while(!int.TryParse(input, out inputNumber))
{
Console.WriteLine("Invalid input");
DoFactorial();
}
int factorial = FindFactorial(inputNumber);
return factorial;
}
private static int FindFactorial(int input)
{
if (input == 1)
{
return input;
}
else
{
return input * FindFactorial(input - 1);
}
}
private static double DoCircleArea()
{
Console.WriteLine("Please enter radius of the circle : ");
string input = Console.ReadLine();
int radius;
while (!int.TryParse(input, out radius))
{
Console.WriteLine("Invalid input");
DoCircleArea();
}
double area = pi * radius * radius;
return area;
}
private static float DoRealConversion(out float realValue2)
{
Console.WriteLine("Please enter two integers separated by comma : ");
string[] input = Console.ReadLine().Split(',');
int value1, value2 = 0;
while (!int.TryParse(input[0], out value1) || !int.TryParse(input[1], out value2))
{
Console.WriteLine("Invalid input");
DoRealConversion(out realValue2);
}
realValue2 = (float)value2;
return (float)value1;
}
}
}