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






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

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

           
       








Related examples in the same category

1.Simplest forSimplest for
2.For loop for array
3.The finished C# statement Help system that processes multiple requestsThe finished C# statement Help system   
   that processes multiple requests
4.Determine smallest single-digit factorDetermine smallest single-digit factor
5.A negatively running for loopA negatively running for loop
6.Determine if a number is prime. If it is not, then display its largest factorDetermine if a number is prime.  If it is not, 
   then display its largest factor
7.loop with letter char as the control variable
8.Use commas in a for statememtUse commas in a for statememt
9.Loop condition can be any bool expressionLoop condition can be any bool expression
10.Parts of the for can be emptyParts of the for can be empty
11.Move more out of the for loopMove more out of the for loop
12.The body of a loop can be emptyThe body of a loop can be empty
13.Declare loop control variable inside the forDeclare loop control variable inside the for
14.Using break to exit a loopUsing break to exit a loop
15.Use continueUse continue
16.Demonstrate the for loopDemonstrate the for loop
17.Compute the sum and product of the numbers from 1 to 10. Compute the sum and product of the numbers from 1 to 10.
18.For Loop TesterFor Loop Tester
19.If inside a forIf inside a for
20.For without init valueFor without init value
21.For without increaseFor without increase
22.For with empty conditionFor with empty condition
23.a for loop to display 1 to 5a for loop to display 1 to 5
24.Update two parameters in for loop