Prime Number - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not.

A prime number is one that has exactly two positive divisors: 1 and itself. The program should check if the entered number satisfies this property. To do so, the program will evaluate the possible divisors of the number, and if any divisor other than 1 or the number itself is found, the number will be concluded as not prime. This exercise is useful for understanding how to implement prime number checking algorithms and practicing the use of loops and conditionals in C#. Additionally, it provides a great opportunity to learn about the use of division in programming and how to optimize divisor searches by checking only up to the square root of the number.

 Category

Flow Control

 Exercise

Prime Number

 Objective

Write a C# program that asks the user for an integer and determines if it is a prime number or not.

 Write Your C# Exercise

using System;  // Import the System namespace, which contains fundamental classes like Console

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

        // Check if the number is less than 2, as prime numbers are greater than 1
        if (number < 2)  
        {
            Console.WriteLine("The number is not prime.");  // If the number is less than 2, it's not prime
        }
        else  
        {
            bool isPrime = true;  // Assume the number is prime unless proven otherwise

            // Check divisibility from 2 to the square root of the number
            for (int i = 2; i <= Math.Sqrt(number); i++)  
            {
                if (number % i == 0)  // If the number is divisible by i, it's not prime
                {
                    isPrime = false;  // Set isPrime to false if a divisor is found
                    break;  // Exit the loop as we've found a divisor
                }
            }

            // Display whether the number is prime or not
            if (isPrime)  
            {
                Console.WriteLine("The number is prime.");  // If no divisors were found, it's prime
            }
            else  
            {
                Console.WriteLine("The number is not prime.");  // If a divisor was found, it's not prime
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

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#.

  •  Give change

    This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program s...

  •  Exceptions

    This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors...

  •  Positive and negative

    In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...

  •  Multiply if not zero

    In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...

  •  Divide if not zero

    In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...

  •  Divide if not zero (Using else)

    In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...