This is the best logic building program for beginners in C#. The purpose of this code is to demonstrate the use of functions with array as a parameter in C#. The program will finds the greatest smallest and average of given numbers using array as a parameter.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace minMax
{
class Program
{
static void Main(string[] args)
{
welcomeScrean();
int size; //Array size
Console.Write("\n\n\tHow many numbers do you want to input? ");
size = Convert.ToInt32(Console.ReadLine());
float[] dataArray = new float[size]; //Declaring dataArray
for (int index = 0; index < size; index++) //Loop which inputs arrays data
{
Console.Write("\n\tNumber " + (index + 1) + " is: ");
dataArray[index] = Convert.ToInt32(Console.ReadLine());
}
//Now calling division function to find the sum...
Console.Write("\n\tAverage of dataArray elements is " + division(dataArray, size) );
minMax(dataArray, size);
string s = Console.ReadLine();
return;
}
private static void welcomeScrean()
{
Console.Write("\n"
+ "\a"
+ "\t *******************************************************" + "\n"
+ "\t ** Welcome **" + "\n"
+ "\t *******************************************************" + "\n");
}
public static float division(float[] dataArray, int size)
{
float sum = 0.0F;
float average = 0;
for (int index = 0; index < size; index++)
sum += dataArray[index];
average = sum / size;
return average;
}
public static void minMax(float[] dataArray, int size)
{
int max;
int min;
max = Convert.ToInt32(dataArray[0]);
min = Convert.ToInt32(dataArray[0]);
for (int index = 0; index < size; index++)
{
if (dataArray[index] > max)
{
max = Convert.ToInt32(dataArray[index]);
}
if (dataArray[index] < min)
{
min = Convert.ToInt32(dataArray[index]);
}
}
Console.Write("\n\tBiggest number is : " + max);
Console.Write("\n\tSmallest number is: " + min + "\n");
}
}
}
Output of the Program
Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of
BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
0 comments:
Post a Comment