Prime Factors - C# Programming Exercise

In this C# exercise, you are asked to write a program that takes a number entered by the user and displays it as a product of its prime factors. Prime factors are numbers that are divisible only by 1 and themselves. For example, for the number 60, its prime factorization would be: 60 = 2 · 2 · 3 · 5.

Additionally, the exercise includes a hint that makes it easier to display the result: 60 = 2 · 2 · 3 · 5 · 1, which helps to provide a consistent format when displaying the prime factors. This exercise allows you to practice using loops, conditions, and programming logic to calculate the prime factors of a given number, which is essential in factorization algorithms.

The program should take an integer, divide it successively by the smaller prime numbers until it is no longer divisible, and then display the original number as the product of those prime factors. You will also learn to manage the output format so that it is displayed clearly and neatly in the console.

This type of exercise is very useful for understanding how factorizations work in mathematics and their implementation in programming, as well as reinforcing your understanding of prime numbers and their application in divisibility algorithms.

 Category

Basic Data Types

 Exercise

Prime Factors

 Objective

Write 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)

 Write Your C# Exercise

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        // Ask the user to enter a number
        Console.Write("Enter a number: ");  // Display prompt for the number input
        int number = int.Parse(Console.ReadLine());  // Read the input as an integer

        int originalNumber = number;  // Store the original number for display later
        string result = originalNumber + " = ";  // Start the result string

        // Check for divisibility by 2 first (for efficiency)
        while (number % 2 == 0)  // While the number is divisible by 2
        {
            result += "2 · ";  // Append '2' to the result string
            number /= 2;  // Divide the number by 2
        }

        // Now check for odd factors starting from 3
        for (int i = 3; i <= Math.Sqrt(number); i += 2)  // Only check odd numbers (i += 2)
        {
            while (number % i == 0)  // While the number is divisible by 'i'
            {
                result += i + " · ";  // Append the factor 'i' to the result string
                number /= i;  // Divide the number by 'i'
            }
        }

        // If the remaining number is greater than 2, it must be prime
        if (number > 2)
        {
            result += number + " · ";  // Append the last prime factor
        }

        // To remove the final " · " from the result string
        result = result.Substring(0, result.Length - 2);  // Remove last two characters

        // Display the result
        Console.WriteLine(result);  // Print the prime factorization of the number
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  If, symbols

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is an uppercase vowel, a lowercase vowel, a digit...

  •  Char + for

    In this C# exercise, you are asked to write a program that prints the letters from B to N in uppercase, using a for loop. This type of exercise is perfect for ...

  •  Double, approximation of Pi

    In this C# exercise, you are asked to write a program that calculates an approximation for PI using the alternating fraction series: pi/4 = 1/1 - 1/3 + 1/5 -...

  •  Perimeter Area

    In this C# exercise, you are asked to write a program that calculates the perimeter, area, and diagonal of a rectangle from its width and height....

  •  Hexadecimal and binary

    In this C# exercise, you are asked to write a program that prompts the user for a number and displays it in both hexadecimal and binary. The program should continue r...

  •  Binary

    In this C# exercise, you are asked to write a program that prompts the user for a decimal number and displays its equivalent in binary. The program should continue re...