In: Computer Science
Method calling in c#
I need to write methods for calling the min and max numbers using this code;
Console.WriteLine("Calling highest method.");
Console.WriteLine("Highest number is: {0}", highest(3));
Console.WriteLine("Calling lowest method.");
Console.WriteLine("Lowest number is: {0}", lowest(3));
Program1:
//Finding the highest and lowest number from an array with given size 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsDemo
{
class Program
{
public static int highest(int num)
{
int[] arr = new int[num];
Console.WriteLine("Enter 3 numbers: ");
for (int i = 0; i <= 2; i++)
{
arr[i]=Convert.ToInt32(Console.ReadLine());
}
int high = arr[0];
for (int i=0;i<=2;i++)
{
if (high < arr[i])
{
high = arr[i];
}
}
return high;
}
public static int lowest(int num)
{
int[] arr = new int[num];
Console.WriteLine("\nEnter 3 numbers: ");
for (int i = 0; i <= 2; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
int low = arr[0];
for (int i = 0; i <= 2; i++)
{
if (low > arr[i])
{
low = arr[i];
}
}
return low;
}
static void Main(string[] args)
{
Console.WriteLine("Calling highest method.");
Console.WriteLine("Highest number is: {0}", highest(3));
Console.WriteLine("Calling lowest method.");
Console.WriteLine("Lowest number is: {0}", lowest(3));
Console.ReadKey();
}
}
}
Program2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsDemo
{
class Program
{
public static int highest(int high)
{
return high;
}
public static int lowest(int low)
{
return low;
}
static void Main(string[] args)
{
Console.WriteLine("Calling highest method.");
Console.WriteLine("Highest number is: {0}", highest(3));
Console.WriteLine("Calling lowest method.");
Console.WriteLine("Lowest number is: {0}", lowest(3));
Console.ReadKey();
}
}
}
Note:
In the given question you didn't mention what we have to do so i am going with 2 programs with given contents.
I hope that will be helpful.