Prime factors Learn programming C#

Lesson:

Basic Data Types


Exercise:

Prime factors


Objetive:

Create a C# program that displays a number (entered by the user) as a product of its prime factors. For example, 60 = 2 · 2 · 3 · 5

(Hint: it can be easier if the solution is displayed as 60 = 2 · 2 · 3 · 5 · 1)


Code:

using System;
public class exercise63
{
    public static void Main()
    {
        int n;
        int d = 2;

        Console.Write("Enter the number: ");
        n = Convert.ToInt32(Console.ReadLine());

        while (n > 1)
        {
            while (n % d == 0)
            {
                Console.Write(d);
                Console.Write(" · ");
                n = n / d;
            }

            d++;
        }
        Console.Write(1);
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.