Advertisement
Qrist

Find least and greatest common factors

Apr 29th, 2020
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2.  class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         Class ob = new Class();
  7.         int lcf, gcf;
  8.         if (ob.HasComFactor(231, 105, out lcf, out gcf))
  9.         {
  10.             Console.WriteLine("Lcf of 231 and 105 is " + lcf);
  11.             Console.WriteLine("Gcf of 231 and 105 is " + gcf);
  12.         }
  13.         else
  14.             Console.WriteLine("No common factor for 35 and 49.");
  15.         if (ob.HasComFactor(35, 51, out lcf, out gcf))
  16.         {
  17.             Console.WriteLine("Lcf of 35 and 51 " + lcf);
  18.             Console.WriteLine("Gcf of 35 and 51 is " + gcf);
  19.         }
  20.         else
  21.             Console.WriteLine("No common factor for 35 and 51.");
  22.     }
  23. }
  24.  
  25. public class Class
  26. {
  27.     public bool HasComFactor(int x, int y, out int least, out int greatest)
  28.     {
  29.         int max = x < y ? x : y;
  30.         bool first = true;
  31.         least = 1;
  32.         greatest = 1;        
  33.         for (int i = 2; i <= max / 2 + 1; i++)
  34.         {
  35.             if (((y % i) == 0) & ((x % i) == 0))
  36.             {
  37.                 if (first)
  38.                 {
  39.                     least = i;
  40.                     first = false;
  41.                 }
  42.                 greatest = i;
  43.             }
  44.         }
  45.         if (least != 1) return true;
  46.         else return false;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement