C# program to check if a number is prime or not
- Home
- Check value
- C# program to check if a number is prime or not
- On
- By
- 0 Comment
- Categories: Check value, prime
C# program to check if a number is prime or not
C# program to check if a number is prime or not
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…
Example to check whether a Number is Prime or Not
Check the prime number – variable initialization
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
Check the prime number – Entered by the user
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