Sunday, October 30, 2011

GCD (greatest common deviser) Consecutive integers and Uclide method


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();
        }
    }
}

4 comments:

  1. Consicutive integaer checking in c language, please send me

    ReplyDelete
    Replies
    1. #include
      #include

      int main()
      {
      int a,b,min;

      printf("Enter 2 numbers\n");
      scanf("%d %d", &a,&b);

      min= (a0){
      if((a%min==0) && (b%min==0)){
      printf("GCD = %d \n",min );
      exit(0);
      }
      min--;

      }

      return 0;

      }

      Delete
    2. before the if statement I had used conditional operator which is omitted here

      Delete
  2. Great and I have a swell offer: Does Renovation Increase House Value house renovation and design

    ReplyDelete

used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...