Use commas in a for statememt to find the largest and smallest factor of a number : for : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » for »

 

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); 
  } 
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» for