TheQuack45

mostFactors

Feb 11th, 2016
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace mostFactorsCheck
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Input `x` value: ");
  14.             int xVal = Int32.Parse(Console.ReadLine());
  15.             Console.WriteLine("Input `y` value: ");
  16.             int yVal = Int32.Parse(Console.ReadLine());
  17.  
  18.             int currentFactorsCount;
  19.             int mostFactorsNumber = 0;
  20.             int mostFactorsCount = 0;
  21.             for (int i = xVal; i <= yVal; i++)
  22.             {
  23.                 currentFactorsCount = countFactors(i);
  24.                 if (currentFactorsCount > mostFactorsCount)
  25.                 {
  26.                     mostFactorsNumber = i;
  27.                     mostFactorsCount = currentFactorsCount;
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine("Between {0} and {1}, " + mostFactorsNumber + " has the most factors with " + mostFactorsCount + " factors.", xVal, yVal);
  32.  
  33.             Console.ReadKey();
  34.         }
  35.  
  36.         public static int countFactors(int x)
  37.         {
  38.             int count = 0;
  39.             for (int i = 1; i <= x; i++)
  40.             {
  41.                 if (x % i == 0)
  42.                 {
  43.                     count++;
  44.                 }
  45.             }
  46.  
  47.             return count;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment