Mega Code Archive

 
Categories / C# / Language Basics
 

Use commas in a for statememt to find the largest and smallest factor of a number

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /*     Use commas in a for statememt to find     the largest and smallest factor of a number.  */    using System;    public class Comma1 {       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);    }  }