## Opt In (Do Not Edit Here)

Showing posts with label Console Application. Show all posts
Showing posts with label Console Application. Show all posts

## The Euclidean Algorithm (GCD Algorithm)

Description:
The blog is intended to demonstrate the Euclidean Algorithm, used to find Greatest Common Divisor (GCD) value of Two Numbers (the oldest Algorithm known, it appeared in Euclid’s Elements around 300 BC).

GCD Value: - Is the Largest number dividing the both numbers

Justification:

Possibility #1:

Our aim is to find GCD (a, b) of two numbers a and b.
Suppose a is smaller than b…..
1.    To find GCD, divide the b by a, if we get reminder zero then …..We done it….B’coz b is multiple of a.
2.    If NOT then again divide the Divisor by Reminder until we get reminder equal to zero.
3.    The Last Non-Zero reminder is the GCD value of a and b.

Step 2:
In Step 1, we mention only one possibility of Euclidean Algorithm, but there are two more      possibilities to find GCD Value between two numbers.

Possibility #2:   a == b          [a exactly equal to b]
Possibility #3:   a > b            [a greater than b]

Here I implemented the Code for all these Three possibilities

The Code:
1. Accept two numbers from User. Declare the variable reminder = 1.

Console.Write("\n\t\tValue-1 : ");
Console.Write("\n\t\tValue-2 : ");
long reminder = 1;
Listing 1
2. The Code for Possibility #1 (a < b)
if (x > y)
{
if (x % y == 0) { reminder = y; }
else
{
reminder = x % y;
if (reminder != 0) { reminder = reminder % y; }
}
}
Listing 2

3. The Code for Possibility #2 (a == b) & Possibility #3 (a > b)

else if (x == y || x < y)
{
if (y % x == 0) { reminder = x; }
else
{
reminder = y % x;
if (reminder != 0) { reminder = reminder % x; }
}
}
Listing 3

4. Now execute the Application and see the result (Figure 1).

Intended Result:

Figure 1

Summary:

In this piece of writing, we have seen the implementation of The Euclidean Algorithm. For writing the code here I used the C# Language.

## Console Application

Introduction:

· A Console Application runs from the command or DOS prompt.

· Console Applications are written in code and are supported by the System.Console namespace.

· A Console Application is simpler than a Windows application.

· It has no graphical user interface.

· Console Applications define standard input, standard output, and standard error streams that the program can use to read and write information to and from the console.

· Console Applications typically do not have any specialized groupings and do not have any events.

· Console Applications are based on Visual Basic modules, which are specifically designed to hold code that is not attached to any form or other such class.

· Console applications are convenient for testing short code segments

Situations where Console Applications are used:

o The Middle-Tier of a Multitiered Application

o A Service running in the background

o A Test application

Limitations of Console Applications:

o Cannot display information in labels, message boxes, etc.

o No Graphical User Interface, hence the IDE looks a bit different.