Find the number of factors (count of factors) and the actual factors of a given number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int numoffact = 0;
FindingFactors ff = new
FindingFactors();
Console.Write("Enter
the number to which factors are needed : ");
int num = Convert.ToInt32(Console.ReadLine());
if (num <= 0)
{
Console.WriteLine("The number should be 1 or above. Exiting...");
Console.ReadLine();
return;
}
int[] allfactors = ff.factors(num, out numoffact);
Console.WriteLine("The
number of factors are : " + numoffact);
Console.Write("The
factors are : ");
for (int i = 0; i
< numoffact; i++)
{
Console.Write(allfactors[i] + " ");
}
Console.ReadLine();
}
}
class FindingFactors
{
public int[] factors(int number, out int numberoffactors)
{
numberoffactors = 0;
int[] fact = new int[100];
int j = 0; //This is for
array count in fact
for (int i = 1; i
<= number; i++)
{
if (number % i == 0)
{
fact[j] = i;
j = j + 1;
numberoffactors++;
}
}
return fact;
}
}
}
Output :