/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use two out parameters.
using System;
class Num {
/* Determine if x and v have a common denominator.
If so, return least and greatest common denominators in
the out parameters. */
public bool isComDenom(int x, int y,
out int least, out int greatest) {
int i;
int max = x < y ? x : y;
bool first = true;
least = 1;
greatest = 1;
// find least and treatest common denominators
for(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;
}
}
public class DemoOut {
public static void Main() {
Num ob = new Num();
int lcd, gcd;
if(ob.isComDenom(231, 105, out lcd, out gcd)) {
Console.WriteLine("Lcd of 231 and 105 is " + lcd);
Console.WriteLine("Gcd of 231 and 105 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 49.");
if(ob.isComDenom(35, 51, out lcd, out gcd)) {
Console.WriteLine("Lcd of 35 and 51 " + lcd);
Console.WriteLine("Gcd of 35 and 51 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 51.");
}
}
|