Our List Factors code in C#.
List Factors Class File:
using System;
using System.Collections.Generic;
namespace Arithmetic_CS
{
class ListFactors
{
private List<int> found_factors;
private int find_my_factors;
private int sqrt_range; // Use this range for our loop
public ListFactors(int candidate)
{
found_factors = new List<int>();
find_my_factors = candidate;
sqrt_range = (int)Math.Ceiling(Math.Sqrt(find_my_factors));
}
/**
* Does the main job of finding the requested factors.
* @return - array list of integers
*/
public List<int> doBusiness()
{
/* Loop through 1 to 'find_my_factors' and test for divisibility. */
for (int i = 1; i < sqrt_range; i++)
{
if ((find_my_factors % i) == 0)
{
found_factors.Add(i);
/* Get the complementing factor by dividing 'find_my_factor' by variable i. */
found_factors.Add(find_my_factors / i);
}
}
// Sort the array in ascending order; Not entirely necessary.
found_factors.Sort();
return found_factors;
}
}
}
Main Class:
using System;
using System.Collections.Generic;
namespace Arithmetic_CS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to our demonstration sequels");
Console.WriteLine("Hope you enjoy (and follow) the lessons.");
Console.WriteLine("\r\n");
/*
* List Prime Numbers.
*/
int candidate = 48;
List<int> answer;
ListFactors factors = new ListFactors(candidate);
answer = factors.doBusiness();
Console.WriteLine("Factors of " + candidate + " are:");
Console.WriteLine(String.Join(", ", answer));
}
}
}
Try it out!
Elegance (0.0)