Convert the following program to use methods
C#
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:
C#
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 MakwanaRank #324 · 7.7K reputationPosted Mar 4, 2023, 6:33 AM EST
Hello Divyansha,
Use below code
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(); } static int RequestValue() { Console.Write("Enter a value: "); int value = int.Parse(Console.ReadLine()); return value; } static bool IsEven(int value) { return value % 2 == 0; } static void PrintResult(bool isEven, int value) { if (isEven) { Console.WriteLine($"{value} is even"); } else { Console.WriteLine($"{value} is odd"); } }Thanks
Naimish Makwana
Sachin SinghRank #66 · 26.2K reputationPosted Mar 4, 2023, 9:04 AM EST
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
public class Common { public bool IsEven(int n) { return n%2==0; // any integer which is not even will always be odd so no need to create odd method } }use it inside your console apps as
using System; public class Program { static void Main(string[] args) { Common c= new Common(); int i=0; while(i<3) { Console.WriteLine("Enter a value "); int value = int.Parse(Console.ReadLine()); if (c.IsEven(value)) { Console.WriteLine($"{value} is even"); } else { Console.WriteLine($"{value} is odd"); } i++; } } }you can also create method within UI program class as
using System; public class Program { static void Main(string[] args) { Common c= new Common(); int i=0; while(i<3) { Console.WriteLine("Enter a value "); int value = int.Parse(Console.ReadLine()); if (c.IsEven(value)) { Console.WriteLine($"{value} is even"); } else { Console.WriteLine($"{value} is odd"); } i++; } } public class Common { public bool IsEven(int n) { return n%2==0; // any integer which is not even will always be odd so no need to create odd method } } }