Create a C# Console Program to find all Prime Factors in  a number

Create a C# Console Program to find all Prime Factors in a number

Creating your main method

To create your main method you need to first create the project in Visual Studio Community Edition 2022. First, in the Create a new project window select "Console App".

Give your project a name e.g Factorial

Leave the framework as it is and select next.

right-click on your .cs file and select delete:

right click on your main project file and go to add > class

Next, Name your class and click add

Then you need to remove some of the code from this class. You don't need internal in the class declaration and you don't need most of the system imports.

Keep the following imports so it looks like this:

using System;
using System.Collections.Generic;
namespace PrimeFactorial 
{
    class Prime 
    {

    }
}

Next create the main method:

using System;
using System.Collections.Generic;
namespace PrimeFactorial 
{
    class Prime 
    {
        static void Main(string[] args) 
        {

        }    
    }
}

Creating the main body of code:

First add a Console.WriteLine() inside the main method:

Console.WriteLine("Enter a number:");

Next, add a Console.ReadLine() below the code you have just written:

int number = int.Parse(Console.ReadLine());

Next, add the following code to call upon the FindPrimeFactors method you will make later on and display the prime factors in the console window:

List<int> primeFactors = FindPrimeFactors(number);

if (primeFactors.Count > 0)
{
    Console.WriteLine("The prime factors of " + number + " are:");
    foreach (int factor in primeFactors)
    {
        Console.Write(factor + " ");
    }
}
else
{
    Console.WriteLine("No prime factors found for " + number);
}

Creating the isPrime function

Now, below that you will want to create a function that will calculate whether a number has any prime numbers or not:

static bool IsPrime(int n)
{
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0) return false;
    }
    return true;
}

Creating the FindPrimeFactors method

Finally, below the code you have just written, write the following code to create your FindPrimeFactors method:

static List<int> FindPrimeFactors(int n)
{
    List<int> primeFactors = new List<int>();
    for (int i = 2; i <= n; i++)
    {
        while (IsPrime(i) && n % i == 0)
        {
            primeFactors.Add(i);
            n = n / i;
        }
    }
    return primeFactors;
}

The completed code

Your code is now complete and should look like the following:

using System;
using System.Collections.Generic;

// This is my solution for the Prime Factorial challenge found here:
// https://github.com/karan/Projects

namespace PrimeFactorial
{
    class Prime
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int number = int.Parse(Console.ReadLine());

            List<int> primeFactors = FindPrimeFactors(number);

            if (primeFactors.Count > 0)
            {
                Console.WriteLine("The prime factors of " + number + " are:");
                foreach (int factor in primeFactors)
                {
                    Console.Write(factor + " ");
                }
            }
            else
            {
                Console.WriteLine("No prime factors found for " + number);
            }

        }
        static bool IsPrime(int n)
        {
            if (n <= 1) return false;
            for (int i = 2; i * i <= n; i++)
            {
                if (n % i == 0) return false;
            }
            return true;
        }

        // Finds all prime factors of a nunber in the for loop.
        static List<int> FindPrimeFactors(int n)
        {
            List<int> primeFactors = new List<int>();
            for (int i = 2; i <= n; i++)
            {
                while (IsPrime(i) && n % i == 0)
                {
                    primeFactors.Add(i);
                    n = n / i;
                }
            }
            return primeFactors;
        }
    }
}

I hope this has helped you to create a program that lets you know if a number has prime factors or not in C#.

Please consider sharing this article with someone who is struggling and don't forget you can support me with the sponsor button below.

Did you find this article valuable?

Support Kieran Triggs - Software Developer by becoming a sponsor. Any amount is appreciated!