C# Return Factors of Entered Number

Numbers we can multiply together to get another number.

Example: 2 and 3 are factors of 6, because 2 × 3 = 6

A number can have MANY factors!

Example: What are the factors of 12?
• 3 × 4 = 12, so 3 and 4 are factors of 12
• 2 × 6 = 12, so 2 and 6 are also factors of 12
• and 1 × 12 = 12, so 1 and 12 are factors of 12 as well

So 1, 2, 3, 4, 6 and 12 are all factors of 12
And -1, -2, -3, -4, -6 and -12 also, because multiplying negatives makes a positive.

 

using System;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, x;

            Console.WriteLine("Enter the Number: ");
            num = Convert.ToInt16(Console.ReadLine());  

            Console.WriteLine("\nThe Factors are: ");

            for (x = 1; x <= num; x++)
            {
                if (num % x == 0)
                {
                    Console.WriteLine(x);
                }
            }

            Console.ReadLine();

        }
    }
}

 

Output