Home Programs How to get all factors of a given number in C#

How to get all factors of a given number in C#

In this article we will show you how check the factors of an integer

A factor is a number that divides the given number without any remainder.

Example

 

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number = 0;

            Console.Write("Enter an integer : ");
            number = int.Parse(Console.ReadLine());

            Console.WriteLine("The factors of " + number + " are :");

            for (i = 1; i <= number; i++)
            {
                if (number % i == 0)
                {
                    Console.Write(i + " ");
                }
            }

            Console.ReadLine();
        }
    }
}

Here are some test runs

Enter an integer : 15
The factors of 15 are :
1 3 5 15

Enter an integer : 20
The factors of 20 are :
1 2 4 5 10 20

You may also like