C# File Management Examples - Simple Console Programs for Absolute beginners

Before starting hands on practice for following  C# file management basic programs , I would recommend to please go through a very good article on File I/O.

Basic concepts of File I/O (Link here)

Now since we know little about the following keywords: 


File.CreateText, File.OpenText, File.AppendText, BinaryWriter, StreamReader, StreamWriter

We can easily understand and write the following simple programs.
(All the examples given in this blog have been tested with Visual Studio 2013).


Happy Coding!!:)


Problem 1:

Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt" 

Solution:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exerciseoops1
{
    class Program
    {
        static void Main(string[] args)
        {
                 string sentence = " ";
                 StreamWriter myfile;
                 myfile = File.CreateText("Test1.txt");
                         do
                         {
                             Console.Write("Enter a sentence");
                             sentence = Console.ReadLine();
                             if (sentence.Length != 0)
                             {
                                 myfile.WriteLine(sentence);
                             }
                         }
                         while (sentence.Length != 0);
                         myfile.Close();
                }
        }
    }


Problem 2:


Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt". If the file exists, the new content must be appended to its end.

Solution:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exerciseoops1
{
    class Program
    {
        static void Main(string[] args)
        {
                 string sentence = " ";
                 StreamWriter myfile;
                 myfile = File.CreateText("Test1.txt");
                         do
                         {
                             Console.Write("Enter a sentence");
                             sentence = Console.ReadLine();
                             if (sentence.Length != 0)
                             {
                                 myfile.WriteLine(sentence);
                             }
                         }
                         while (sentence.Length != 0);
                         myfile.Close();
                }
        }
    }


Problem 3:


Create a program to display all the contents of a text file on screen (note: you must use a StreamReader). The name of the file will be entered in the command line or (if there is no command line present) asked to the user by the program

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefile3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the name of the file");
            string nameFile = Console.ReadLine();
            StreamReader myfile;
                try
                {
                    myfile = File.OpenText(nameFile);
                    string line = "";

                            do
                            {
                                line = Console.ReadLine();
                                if (line != null)
                                    Console.WriteLine(line);

                            }
                            while (line != null);

                 }

                catch(Exception e)
                    {
                        Console.WriteLine("Error {0}", e);
                    }
                Console.ReadLine();
        }
    }
}


Problem 4:-


Create a class Logger, with a static method Write, which will append a certain text to a file: 
Logger.Write("myLog.txt", "This text is being logged"); 

It must also include the current date and time before the text (in the same line), so that the log file is easier to analyze. 

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefil3
{
    class Logger
    {

        public static void Write(string nameFile,string text)
        {
            StreamWriter myfile;
            myfile = File.AppendText(nameFile);
            myfile.WriteLine(DateTime.Now + " - " + text);
            myfile.Close();
        }
    }
}

Main Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefil3
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger.Write("text.txt", "hi how are you");
            //Console.ReadLine();
        }
    }
}


Problem 5:


Create a program to count the amount of times that a certain character is inside a file (of any kind). 

The file and the letter can be asked to the user or passed as parameters: 

count example.txt a 

It must display in screen the amount of letters found. 

(you can choose any way to interact with the user, showing the proper help) 

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace Exercisefile8
{
    class Program
    {
        static void Main(string[] args)
        {

            bool debug = true;
            Console.WriteLine("Enter name of file");
            string filename = Console.ReadLine();
            Console.WriteLine("Enter the letter for count");
            string letter = Console.ReadLine();

            StreamReader myfile = File.OpenText(filename);
            string line;

            int countletter = 0;
                do
                {
                    line = myfile.ReadLine();
                    if (line != null)
                    {
                        for (int i = 0; i < line.Length; i++)
                        {
                            if (line.Substring(i, 1) == letter)
                                countletter++;
                        }
                    }
                }
                while (line != null);
                myfile.Close();

                Console.WriteLine("Count of letter {0}", countletter);

                if (debug)
                    Console.ReadLine();

        }
    }
}

Output:






Problem 6:


Create a program which asks the user for his name, his age (byte) and the year in which he was born (int) and stores them in a binary file. 

Create also a reader to test that those data have been stored correctly. 

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefil10
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your name");
            string name = Console.ReadLine();
            Console.WriteLine("Enter your age");
            Byte age = Convert.ToByte(Console.ReadLine());
            Console.WriteLine("Enter your date of Birth");
            int year = Convert.ToInt32(Console.ReadLine());

            BinaryWriter myfile = new BinaryWriter(File.Open("data.dat", FileMode.Create));
            myfile.Write(name);
            myfile.Write(age);
            myfile.Write(year);


            myfile.Close();

            string dataa;
            byte datab;
            int datac;

            BinaryReader file = new BinaryReader(File.Open("data.dat", FileMode.Open));
            dataa = file.ReadString();
            datab = file.ReadByte();
            datac = file.ReadInt32();

            file.Close();

            Console.WriteLine("Name {0} Age {1} Year {2}", dataa, datab, datac);

            if ((dataa != name) || (datab != age) || (datac != year))
                Console.WriteLine("Error in data");
            else
                Console.WriteLine("Read data ok");
            Console.ReadLine();


            
        }
    }
}


 Output:










Problem 7:

 Write a program to read a text file and dump its content to another file, changing the lowercase letters to uppercase. 

You must deliver only the ".cs" file, with you name in a comment. 

Solution:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace Exercisefile16
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter name of the file");
            string filename = Console.ReadLine();
            if(File.Exists(filename))
            {
                StreamReader fileRdr = File.OpenText(filename);
                StreamWriter filewrt = File.CreateText(filename + ".dat");
                string line;
                do{
                    line = fileRdr.ReadLine();
                    if(line!= null)
                    {
                        filewrt.WriteLine(line.ToUpper());

                    }
                }
                while(line!= null);
                fileRdr.Close();
                filewrt.Close();
            }
        }
    }
}


Problem 8:


Create a C# program to count the amount of words stored in a text file 

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefile20
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader infile = File.OpenText("File.txt");
            string line;
            int wordscount = 0;
                do
                {
                    line = infile.ReadLine();
                        if(line != null)
                        {
                            string[] words = line.Split(' ');
                            wordscount += words.Length;
                        }

                }
                 while(line != null);

                Console.WriteLine("no. of Words {0}", wordscount);
                Console.ReadLine();
        }
    }
}



Problem 9:


Create a C# program to tell if two files (of any kind) are identical (have the same content). 

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefile31
{
    class Program
    {
        static void Main(string[] args)
        {
            bool equal = true;
            FileStream myfile1;
            byte[] datafile1;
            FileStream myfile2;
            byte[] datafile2;

            Console.WriteLine("Enter the name of file1");
            String filename1 = Console.ReadLine();
            Console.WriteLine("Enter the name of file2");
            String filename2 = Console.ReadLine();

            if ((!File.Exists(filename1)) || (!File.Exists(filename2)))
                Console.WriteLine("No file exists");
                try
                {
                    myfile1 = File.OpenRead(filename1);
                    datafile1 = new byte[myfile1.Length];
                    myfile1.Read(datafile1,0,(int)myfile1.Length);
                  

                    myfile2 = File.OpenRead(filename2);
                    datafile2 = new byte[myfile2.Length];
                    myfile2.Read(datafile2, 0, (int)myfile2.Length);
                    

                        if ((myfile1.Length) == (myfile2.Length))
                            for (int i = 0; i < datafile1.Length; i++)
                                if (datafile1[i] != datafile2[i])
                                    equal = false;
                        else
                               equal = false;
                        if (equal)
                            Console.WriteLine("File{0} is equal to {1}", filename1, filename2);
                        else

                            Console.WriteLine("File{0} is not equal to {1}", filename1, filename2);
                        myfile1.Close();
                        myfile2.Close();
                        Console.ReadLine();
                   }
            catch(Exception e)
            {
                Console.WriteLine("error" + e.Message);
            }
                Console.ReadLine();
            }
        }
    }



Problem 10:


Create a program to extract (only) the alphabetic characters contained in a binary file, and dump them to a different file. The extracted characters must be those whose ASCII code is 32 to 127, or 10, or 13. 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exercisefile34
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream file;
            Console.WriteLine("Enter the name of the file");
            string name = Console.ReadLine();
            if (!File.Exists(name))
                Console.WriteLine("No File Exists");
                try
                {
                    file = File.OpenRead(name);
                    byte[] bytesfile = new byte[file.Length];
                    file.Read(bytesfile, 0, (int)file.Length);
                    file.Close();
                    StreamWriter newfile = new StreamWriter(name + "01.txt");
                    for(int i =0;i<bytesfile.Length;i++)
                    {
                        if((Convert.ToInt32(bytesfile[i]) >=32 ) && (Convert.ToInt32(bytesfile[i]) <=127)
                            || (Convert.ToInt32(bytesfile[i]) == 10) || (Convert.ToInt32(bytesfile[i]) == 13))
                        {
                            newfile.Write(Convert.ToChar(bytesfile[i]));
                        }
                    }
                    newfile.Close();
                    Console.ReadLine();
                }
                catch(Exception e)
                {
                    Console.WriteLine("Error" + e.Message);
                }
                Console.ReadLine();
        }
    }
}

How To Create Classes, Objects and Constructors in C#

Problem: 

First, create a main program and then define a new class in its own file. Call the class Car. Give it a single method called "Start". Make the method simply print "Car started!". In your main program, create a new Car object and call its Start() method. Your final program should simply therefore display the text "Car started!".

 Solution: 

 Main Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cave7Class
{
    class Program
    {
        static void Main(string[] args)
        {
            Car obj = new Car();
            obj.Start();
        }
    }
}



Class Car


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cave7Class
{
    class Car
    {
        public void Start()
        { 
            Console.WriteLine("Car Started" );
            Console.ReadLine();
        }

    }
}





Constructors 

Problem:

 Modify the above Car class so that it has an instance variable called name of type string Add a constructor that accepts a string parameter and sets the car's name using this parameter. Add a GetName() method that returns the car's name. Finally, modify the main application so that it sets the car's name via the constructor, then prints the cars name (retrieving it using GetName())

 Class Car


using System;




using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cave7Class
{
    class Car
    {
        string name;
        public Car ( string name)
        {
            this.name = name;
        }
        public string Getname()
        {
            return name;
        }
        public void Start()
        { 
            Console.WriteLine("Car started!" );
           
        }

    }
}


Main Program:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cave7Class
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Car car = new Car("My car");

            car.Start();
            Console.WriteLine(car.Getname());
            Console.ReadLine();
        }
    }
}

Output:

Car started!
My Car

How to use Switch Statement in C#

Problem:

Write a program using a switch statement.  that asks the user to enter an integer. If the user enters '1', print "Only one?". If the user enters '100', print "100? That's a lot!". If the user enters something other than these two numbers, print "Input not recognized.". 


Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SwitchStmt
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an Integer");
            int value = 0;
            string option = Console.ReadLine();
            Int32.TryParse(option, out value);
            switch (value)
            {
                case 1:
                    Console.WriteLine("Only one?");
                    break;

                case 100:

                    Console.WriteLine("100? That's a lot!");
                    break;
                default:
                    Console.WriteLine("Input not recognized");
                    break;

            }
            Console.ReadLine();
        }
    }
}

Simple While loop in C#

Write a while loop that asks the user to enter the number '5' and loops over and over until '5' is entered. 

When 5 is finally entered, print "Got it!". 




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Whilecave
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 0;
            while (num != 5)
            {
                Console.WriteLine("Enter number 5");
                string value = Console.ReadLine();
                Int32.TryParse(value, out num);
            }
            Console.WriteLine("Got it!!");
            Console.ReadLine();
        }
    }
}

Do While Loop in C#

Problem:

Write a program that asks the user to enter an integer, then gets the input from the user. The program should repeatedly ask the user to enter an integer until the user enters an integer greater than 10; then it should print "Integer greater than 10 detected!" and should end. The program must contain only two Write or WriteLine statements! 

Solution:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DoWhile
{
    class Program
    {
        static void Main(string[] args)
        {

            int value = 0;
            do
            {
                Console.WriteLine("Enter an Integer");
                string text = Console.ReadLine();
                Int32.TryParse(text, out value);
            }
            while (value <= 10);
            Console.WriteLine("Integer greater than 10 detected!");
            Console.ReadLine();

        }
    }
}

C# Function Examples - Programs for Absolute Beginners

It is always better to get your hands on writing code when learning something new.The beauty of c# language is that half of the work is done by Intellisense. 

In this Blog we will see some C# function programming examples.

Problem 1:



Create a function named "Double" to calculate and return an integer number doubled. For example. Double(7) should return 14. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc8
{
    class Program
    {
        public static int Double(int number)
    {
        return number*2;
    }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter any number");
            int number = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Double of number {0} is {1}", number, Double(number));
            Console.ReadLine();
        }
    }
}


Output:-




Problem 2:


Create a function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must be a "void" function and you must use "refererence parameters". For example. 

x = 5; 
Double(ref x); 
Console.Write(x); 

would display 10

Solution:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc8
{
    class Program
    {
        public static void Double(ref int number)
    {
        number = number +number;
    }
        static void Main(string[] args)
        {
            int x = 5;
            Double(ref x);
            Console.Write(x);
            Console.ReadLine();
        }
    }
}


OutPut:-




Problem 3:-


Create a function named "Swap" to swap the values of two integer numbers, which are passed by reference. 

An example of use might be: 

int x=5, y=3; 
Swap(ref x, ref y); 
Console.WriteLine("x={0}, y={1}", x, y); 
(which should write "x=3, y=5"). 

Solution:--



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc8
{
    class Program
    {
        public static void Swap(ref int x,ref int y)
    {
        int swap;
        swap = x;
        x = y;
        y = swap;

    }
        static void Main(string[] args)
        {
            int x = 5,y=3;
            Swap(ref x,ref y);
            Console.WriteLine("Swaped Values of x ={0}, y = {1}",x,y);
            Console.ReadLine();
        }
    }
}

Output:-






Problem 4:-

Create a program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for the other elements, each one is the sum of the preceding two). 

Solution:-




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc9
{
    class Program
    {
        public static int Fibonacci(int number)
        {
            if ((number == 1) || (number == 2))
                return 1;
            else
                return Fibonacci(number - 1) + Fibonacci(number - 2);
        }
        static void Main(string[] args)
        {
            int number;
            Console.Write("Enter a number");
            number = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Fibonacci of number {0} is {1}", number, Fibonacci(number));
            Console.ReadLine();
        }


    }
}


Output:







Problem 5:


Create a function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not: 
if (isPrime(127)) ...




Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc14
{
    class Program
    {
        public static bool isprime(int n)
        {
            for (int i = 2; i < n; i++)
                if (n % 2 == 0)
                    return false;
            return true;
          
        }
        static void Main(string[] args)
        {
            //int number = Convert.ToInt32(Console.ReadLine());
            if (isprime(97))
                Console.WriteLine("97 number is prime");
            else
                Console.WriteLine("not prime");
            Console.ReadLine();

        }
    }
}


Output:







Problem 6:

Create a recursive function to calculate the factorial of the number specified as parameter: 

Console.Write ( Factorial (6) ); 

would display 720 


Solution:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc18
{
    class Program
    {
        public static int Factorial(int number)
        {
            if (number == 1)
                return 1;
            else
                return number * Factorial(number - 1);

        }
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Enter any number");
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Factorial of number {0} is {1}", n, Factorial(n));
            Console.ReadLine();
        }
    }
}

Output:

 

Problem 7:

Create an iterative (non-recursive) function to calculate the factorial of the number specified as parameter: 

Console.Write ( Factorial (6) ); 

would display 
720
 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc18
{
    class Program
    {
        public static int Factorial(int number)
        {
            int result = 1;
            for (int i = 1; i <= number; i++)
                result = result * i;
            return result;
        }
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Enter any number");
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Factorial of number {0} is {1}", n, Factorial(n));
            Console.ReadLine();
        }
    }
}

Output:







Problem 8: 

 Create a function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number, repeats if the number is not between the minimum value and the maximum value which are indicated as parameters, and finally returns the entered number: 


 age = GetInt("Enter your age", 0, 150);


 would become:


 Enter your age: 180 Not a valid answer. Must be no more than 150. Enter your age: -2 Not a valid answer. Must be no less than 0. Enter your age: 20 (the value for the variable "age" would be 20) 


 Solution :





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc19
{
    class Program
    {
        public static int getInt(string text, int low, int high)
        {
            int answer;
            do
            {
                Console.Write(text);
                answer = Convert.ToInt32(Console.ReadLine());
                if ((answer > high))
                    Console.WriteLine("Not a valid answer. Must be no more than 150");
                if ((answer < low))
                    Console.WriteLine("Not a valid answer. Must be no less than 0");
            } while ((answer < low) || (answer > high));
            return answer;
        }
        static void Main(string[] args)
        {
            int age = getInt("Enter your age:  ", 0, 150);
            Console.WriteLine("The age is {0}", age); 
            Console.ReadLine();
        }
    }
}


OutPut:








Problem 9:

Create a function which draws a parallelogram, with the width, height and character specified as parameters: 

DrawParallelogram(10,4,'*'); 

would display 
********** 
_********** 
__********** 
___********** 

(The symbol _ means a position where a blank space must be displayed)
 



Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc20
{
    class Program
    {
        static void drawparreellogram(int height,int width,char character)
        {
            for(int i=0; i<height; i++)
            {
                for(int j=0;j<width;j++)
                    Console.Write(character);
                Console.WriteLine();
                for(int c=0;c<=i;c++)
                    Console.Write(" ");
            }
        }

        static void Main(string[] args)
        {
            drawparreellogram(10, 4, '%');
            Console.ReadLine();
        }
    }
}


Output:












Problem 10:

Create a function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three parameters: the string that we want to search, the variable that returns the number of digits, and the number of vowels, in that order). The function should be called "CountDV". Use it like this: 

CountDV ("This is the phrase 12", ref amountOfDigits, ref amountOfVowels)

In this case, amountOfDigits would be 2 and amountOfVowels would be 5
 



Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc21
{
    class Program
    {
        public static void CountDV(string answer,ref int amountofDigits,ref int amountofVowels)
        {
           amountofDigits = 0;
             amountofVowels = 0;
            for(int i =0;i<answer.Length;i++)
            {
                switch(answer.Substring(i,1).ToLower())
                {
                    case "a":
                    case "e":
                    case "i":
                    case "o":
                    case "u":
                        amountofVowels++;
                        break;
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        amountofDigits++;
                        break;
                }}
        }
        static void Main(string[] args)
        {
            int amountofDigits = 0;
            int amountofVowels = 0;
            CountDV("this is the phrase 12",ref amountofDigits,ref amountofVowels);
            Console.WriteLine("Amount of digits {0}",amountofDigits);
                        Console.WriteLine("Amount of vowels {0}",amountofVowels);
            Console.ReadLine();


        }
    }
}

Output:




Problem 11:

Create a function that tells if a string is an intenger number. It should be used like this: 

if (IsNumber ("1234")) 
System.Console.WriteLine ("It is a numerical value");

Solution:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc22
{
    class Program
    {
        public static bool isnumber (string text)
    {
            for (int i=0;i<text.Length;i++)
        if((text[i] < '0' ) || (text[i] >'9'))
            return false;
            return true;
    }
        static void Main(string[] args)
        {
            Console.WriteLine("isnumber {0}", isnumber("12"));
            Console.ReadLine();

        }
    }
}

Output:





Problem 12:

Create a function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters: 

float[] data={1.5f, 0.7f, 8.0f} 
MinMaxArray(data, ref minimum, ref maximum); (after that call, minimum would contain 0.7, and maximum would contain 8.0) 


Solution:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc25
{
    class Program
    {
        public static void MinMaxarray(float[] number,ref float min,ref float max)
        {
            max = number[0];
            min = number[0];
                    for(int i=1; i<number.Length;i++)
                    {
                        if (number[i] > max)
                            max = number[i];
                        if (number[i] < min)
                            min = number[i];
                    }



        }
        static void Main(string[] args)
        {
            float[] data = { 3.5f, 0.5f, 9.0f };
            float min = 0.0f;
            float max = 0.0f;
            MinMaxarray(data, ref min, ref max);
            Console.WriteLine("Min {0} Max {1}", min, max);
            Console.ReadLine();
        }
    }
}


Output:








Problem 13:

Create an iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.

Solution:





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc26
{
    class Program
    {
                public static bool isPalindrmoe(string text)
                {
                    text = text.ToUpper();
                    int begin = 0;
                    int end = text.Length - 1;
                            for ( begin =0;begin < end ;begin++)
                            {
                                if (text[begin] != text[end])
                                    return false;
                                end--;
                            }
                            return true;

                    }
            
        
        static void Main(string[] args)
        {
             Console.WriteLine("{0} Palin:",isPalindrmoe("Radar"));
             Console.ReadLine();
        }
    }
}


Output:






Practice programs for C# function to write centered on screen

Problem:

 Create a function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters): WriteCentered("Hello!");

 Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercisefunc5
{
    class Program
    {
        public static void cener(string text)
    {
        int i =0;
        for (;i<17;i++)
        Console.WriteLine();
            for(i = 0;i<36;i++)
            Console.Write(" ");
            Console.Write(text);
            for (i = 0; i < 14; i++)
                Console.WriteLine();

    }
        static void Main(string[] args)
        {
            cener("Hello");
            Console.ReadLine();
        }
    }
}

Output: