Basic Programming C# Examples

C# Program to Determine If Given number is prime or not

The Program in C# Program to Determine If Given number is prime or not is given below:

using System;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, Enter a number here  to check if it is a prime number: ");
            int number = int.Parse(Console.ReadLine());

            if (IsPrime(number))
            {
                Console.WriteLine(number + " is a prime number.");
            }
            else
            {
                Console.WriteLine(number + " is not a prime number.");
            }

            Console.ReadLine();
        }

        static bool IsPrime(int num)
        {
            if (num <= 1)
            {
                return false;
            }

            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

Output:

Hello Codeauri Family, Enter a number here to check if it is a prime number:
7
7 is a prime number.

Pro-Tips💡

Here are the step by step execution of above program:

  1. The program starts by including the System namespace using the using keyword.
  2. The code is contained within a namespace called PrimeNumber. This allows the code to be organized and reused easily in other projects.
  3. The Program class contains the main method Main, which is the starting point of the program.
  4. Within the Main method, the user is prompted to enter a number using the Console.WriteLine method. The user’s input is then converted to an int using int.Parse and stored in the number variable.
  5. The IsPrime method is then called and passed the number as an argument. The method returns a bool value, which is stored in a variable result.
  6. The Main method checks the value of result. If result is true, the message “The number is a prime number.” is displayed on the console. If result is false, the message “The number is not a prime number.” is displayed.
  7. The Console.ReadLine method is used to pause the program and wait for the user to press Enter.
  8. The IsPrime method checks if the number is less than or equal to 1. If it is, the method returns false, as 1 is not a prime number and numbers less than 1 are not considered valid numbers.
  9. The method then loops through all the numbers from 2 to the square root of the input number using a for loop.
  10. Within the loop, the method checks if the input number is divisible by the current loop variable i by using the modulo operator %. If it is divisible, the method returns false, as the number is not a prime number.
  11. If the method completes the loop without finding any factors, it returns true as the number is a prime number.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X