Algorithm
to find GCD (greatest common deviser)
Consecutive
integers and Uclide method
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace algorithm
{
class GCDalgos
{
int t;
int r;
int
size;
public
GCDalgos() { }
//computer
gcd of m and n value by uclide method
//input: two
non-negative integers and non zero
//output: the
greates common diviser of m and n value
public int uclide(int _m,int size)
{
if
(size > _m)
{
int
temp = size;
size = _m;
_m = temp;
}
if
(size == 0 || size ==1)
{ Console.WriteLine("the GCD By uclid is: {0}",_m);}
else
{
r = _m % size;
_m = size;
size = r;
uclide(_m, size);
}
return
0;
}
//computer
gcd by consecutive integer method of m and n
//input: two
non-negative integers and non zero
//output: the
greates common diviser of m and n value
public int consecutiveinteger(int
m, int n)
{
//if (m
== 0 || n == 0) { return m; }
if
(m < n)
{
t = m;
}
else
{
t = n;
}
while
(t != 1)
{
if
(m % t == 0)
{
if
(n % t == 0)
{
return t;
}
t = t - 1;
}
else
t = t-1;
}
return
m;
}
}
class Program
{
static void Main(string[]
args)
{
GCDalgos a = new GCDalgos();
Console.WriteLine("Enter value of m");
int
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of n");
int
m = Convert.ToInt32(Console.ReadLine());
a.uclide(m, n);
Console.WriteLine("The GCD by Consecutive integer is: {0}",
a.consecutiveinteger(m, n));
Console.Read();
}
}
}