Find the largest and smallest factor of a number in CSharp
Description
The following code shows how to find the largest and smallest factor of a number.
Example
using System; // w ww . j a v a 2s .c om
public class MainClass {
public static void Main() {
int i, j;
int smallest, largest;
int num;
num = 100;
smallest = largest = 1;
for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--) {
if((smallest == 1) & ((num % i) == 0))
smallest = i;
if((largest == 1) & ((num % j) == 0))
largest = j;
}
Console.WriteLine("Largest factor: " + largest);
Console.WriteLine("Smallest factor: " + smallest);
}
}
The code above generates the following result.