C# Return Prime Factors of Given Number

Prime Factor any of the prime numbers that can be multiplied to give the original number.

Example: The prime factors of 15 are 3 and 5 (because 3×5=15, and 3 and 5 are prime numbers).

 

using System;

namespace prime
{
    public class ReturnPrimeFactors
    {
        // Entry Point
        public static void Main()
        {
            Console.WriteLine("Enter number: ");
            int userInput = Convert.ToInt32(Console.ReadLine());

            PrimeFactors(userInput);

            Console.Read();
        }

        
        public static void PrimeFactors(int n)
        {
            while (n % 2 == 0)
            {
                Console.Write(2 + " ");
                n /= 2;
            }

           for (int i = 3; i <= Math.Sqrt(n); i += 2) { while (n % i == 0) { Console.Write(i + " "); n /= i; } } if (n > 2)
                // output factors
                Console.Write(n);
        }        

    }
}

 

Output