Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- Class ob = new Class();
- int lcf, gcf;
- if (ob.HasComFactor(231, 105, out lcf, out gcf))
- {
- Console.WriteLine("Lcf of 231 and 105 is " + lcf);
- Console.WriteLine("Gcf of 231 and 105 is " + gcf);
- }
- else
- Console.WriteLine("No common factor for 35 and 49.");
- if (ob.HasComFactor(35, 51, out lcf, out gcf))
- {
- Console.WriteLine("Lcf of 35 and 51 " + lcf);
- Console.WriteLine("Gcf of 35 and 51 is " + gcf);
- }
- else
- Console.WriteLine("No common factor for 35 and 51.");
- }
- }
- public class Class
- {
- public bool HasComFactor(int x, int y, out int least, out int greatest)
- {
- int max = x < y ? x : y;
- bool first = true;
- least = 1;
- greatest = 1;
- for (int i = 2; i <= max / 2 + 1; i++)
- {
- if (((y % i) == 0) & ((x % i) == 0))
- {
- if (first)
- {
- least = i;
- first = false;
- }
- greatest = i;
- }
- }
- if (least != 1) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement