Convert the following program to use methods
static void Main(string[] args)
{
Console.Write("Enter a value ");
int value = int.Parse(Console.ReadLine());
if (value % 2 == 0)
{
Console.WriteLine($"{value} is even");
}
else
{
Console.WriteLine($"{value} is odd");
}
Console.Write("Enter a value ");
value = int.Parse(Console.ReadLine());
if (value % 2 == 0)
{
Console.WriteLine($"{value} is even");
}
else
{
Console.WriteLine($"{value} is odd");
}
Console.Write("Enter a value ");
value = int.Parse(Console.ReadLine());
if (value % 2 == 0)
{
Console.WriteLine($"{value} is even");
}
else
{
Console.WriteLine($"{value} is odd");
}
}
To be like this:
static void Main(string[] args)
{
int value = RequestValue();
bool isEven = IsEven(value);
PrintResult(isEven, value);
value = RequestValue();
isEven = IsEven(value);
PrintResult(isEven, value);
value = RequestValue();
isEven = IsEven(value);
PrintResult(isEven, value);
Console.ReadLine();
}
Naimish MakwanaPosted Mar 4, 2023, 11:33 AM
Hello Divyansha,
Use below code
Thanks
Naimish Makwana
Sachin SinghPosted Mar 4, 2023, 2:04 PM
Always keep common methods or Business Logic method's loosely coupled with UI , this means while creating method don't use any keywords that are specific to a particular UI, for example Console.ReadLine() or WritleLine() methods are specific to Console Apps so if you will use those methods inside your user defined (custom) methods then you won't be able to use them or call them from windows app, mobile app or MVC web app or web forms.
so,
first create a class library project inside your solution and inside that create a class , name it as common and create methods as
use it inside your console apps as
you can also create method within UI program class as