usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

C# Program to Calculate HCF (GCD) with User Input - Kids Fun Project



Understanding the HCF Algorithm in C#

This C# program calculates the HCF (Highest Common Factor) using user input and the Fast HCF C# Code from the previous lesson.
Let's add some input mechanism so our user enters the set of numbers whose H.C.F. we are to find.


Combining the H.C.F. (G.C.D.) C# Code and Collecting User Input

All we need is a main C# code that asks the user for input.
For this purpose, we'll use the Console.ReadLine() library function.

How to use the HCF Code in C#

The user gets to enter his/her choice of numbers for the HCF C# algorithm.
After entering each number, the user presses <Enter>.
After the user has entered every of his/her numbers, the user enters the word done to see the result.


Note: You can comment out the C# code for the main class from the previous lesson if you have been following.



So! C# Fun Practice Exercise - Find HCF with User Input

As a fun practice exercise, feel free to try out your own numbers, and see how the C# code finds the HCF of your input numbers.







C# Code for HCF with User Input - Main Class.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Arithmetic
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our demonstration sequels");
            Console.WriteLine("Hope you enjoy (and follow) the lessons.");
            Console.WriteLine("\r\n");

            /*
            * Collect input.
             */

            // Collect input
            string collect_input; // For collecting user input

            List<int> set = new List<int>();

            Console.WriteLine("Welcome to our Find H.C.F. program.");
            Console.WriteLine("Enter your series of number for H.C.F.");
            Console.WriteLine("Type 'done' when you are through with entering your numbers.\n");

            string collect_input_text = "Enter First Number:  ";
            try
            {
                do
                {
                    // Get keyboard input
                Console.Write(collect_input_text);
                    collect_input = Console.ReadLine();
                    if (Regex.IsMatch(collect_input, @"[0-9]+"))
                    {
                        if (int.Parse(collect_input) != 0)
                        {
                            set.Add(int.Parse(collect_input));
                            collect_input_text = "Enter Next Number:  ";
                        }
                        else
                        {
                            Console.WriteLine("You cannot enter zero. Repeat that!Type 'done' when you're finished.");
                        }
                    }
                    else if (!"DONE".Equals(collect_input.ToUpper()))
                    {
                        Console.WriteLine("Wrong Input. Repeat that!\r\n Type 'done' when you're finished.");
                    }
                } while (!"DONE".Equals(collect_input.ToUpper()));
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }

            FastHCF h_c_f = new FastHCF(set);
            Console.WriteLine("The H.C.F. of " + String.Join(", ", set) + " is " + h_c_f.getHCF());

        }
    }
}



<< PreviousNext >>