In this tutorial, we will discuss the concept of the C# program to check whether a Number is Prime or Not
In this post, we are going to learn how to check whether a number is prime or not in Csharp programming language with Example program
Prime number
The number that can be divided by 1 and itself ony. it is called a prime number
for Example 2,3,5,7,11,13…
In this program, we will initialize the variable as a positive number, then it will check whether the given number is a prime number or not, using the for loop in C# language.
Program 1
//c# program to check if a number is prime or not using System; public class CheckPriume{ public static void Main(string[] args) { int num=7,count=0; for (int i=1; i<=num; i++){ if(num%i==0){ count++; } } if(count==2){ Console.WriteLine ("{0} is a prime number",num); }else{ Console.WriteLine ("{0} is noy a prime number",num); } } }
When the above code is executed, it produces the following result
7 is a prime number
This program allows the user to enter a positive number then it will check whether the given number is a prime number or not, using the for loop in C# language.
Program 2
//c# program to check if a number is prime or not //Entered by user using System; public class CheckPriume{ public static void Main(string[] args) { int num,count=0,i; Console.WriteLine ("Enter the number to check prime or not"); num=int.Parse( Console.ReadLine()); int m=num/2; for (i=2; i<=m; i++){ if(num%i==0){ Console.WriteLine ("{0} is not a prime number",num); count=1; break; } } if(count==0){ Console.WriteLine ("{0} is a prime number",num); } } }
When the above code is executed, it produces the following result
Case 1
Enter the number to check prime or not 17 17 is a prime number
Case 2
Enter the number to check prime or not 10 10 is not a prime number
Similar post
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…