C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to Check Prime Number
Photo Credit to CodeToFun
π Introduction
In the landscape of programming, dealing with prime numbers is a fundamental and interesting task. Prime numbers, those divisible only by 1 and themselves, play a crucial role in various mathematical and computational applications.
In this tutorial, we will delve into a simple yet effective C# program to check whether a given number is a prime number.
π Example
Let's explore the C# code that performs the prime number check.
using System;
class Program {
// Function to check if a number is prime
static bool IsPrime(int number) {
// 0 and 1 are not prime numbers
if (number <= 1) {
return false;
}
// Check for factors from 2 to the square root of the number
for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) {
return false; // Found a factor, not a prime number
}
}
return true; // No factors found, it's a prime number
}
// Driver program
static void Main() {
// Replace this value with your desired number
int testNumber = 17;
// Call the function to check if the number is prime
if (IsPrime(testNumber)) {
Console.WriteLine($"{testNumber} is a prime number.");
} else {
Console.WriteLine($"{testNumber} is not a prime number.");
}
}
}
π» Testing the Program
To test the program with a different number, replace the value of testNumber in the Main method.
17 is a prime number.
Compile and run the program to see whether the number is a prime number.
π§ How the Program Works
- The program defines a function IsPrime that takes an integer as input and returns a boolean indicating whether the number is prime.
- Inside the function, it checks if the number is less than or equal to 1; if so, it's not prime.
- It then iterates through potential factors from 2 to the square root of the number.
- If it finds any factor, the number is not prime; otherwise, it is prime.
- The Main method tests the program with a specific number (in this case, 17).
π Between the Given Range
Let's dive into the C# code that checks and lists prime numbers in the specified range.
using System;
class Program {
// Function to check if a number is prime
static bool IsPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.Sqrt(number); ++i) {
if (number % i == 0) {
return false;
}
}
return true;
}
// Driver program
static void Main() {
Console.Write("Prime Numbers in the Range 1 to 20: \n");
// Check and list prime numbers in the range 1 to 20
for (int i = 1; i <= 20; ++i) {
if (IsPrime(i)) {
Console.Write(i + " ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
The program is set to check and list prime numbers in the range from 1 to 20. No additional input is required.
Prime Numbers in the Range 1 to 20: 2 3 5 7 11 13 17 19
Compile and run the program to see the prime numbers in the specified range.
π§ How the Program Works
- The program defines a function IsPrime that checks whether a given number is prime.
- Inside the function, it iterates through numbers from 2 to the square root of the given number to check for divisors.
- The Main method then iterates through the range from 1 to 20, calling the IsPrime function to determine and print the prime numbers.
π§ Understanding the Concept of Prime Numbers
Before diving into the code, let's briefly understand prime numbers.
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
π’ Optimizing the Program
While the provided program is effective, you can explore optimizations such as using the square root of the number as the loop limit for improved efficiency.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to Check Prime Number), please comment here. I will help you immediately.