C# function to check a number is prime or not
- Home
- Check value
- C# function to check a number is prime or not
- On
- By
- 0 Comment
- Categories: Check value, function/method, prime
C# function to check a number is prime or not
C# function to check whether a number is prime or not
In this tutorial, we will discuss the concept of the C# proram to check whether a Number is Prime or Not – using a user-defined function
In this post, we are going to learn how to check whether a number is prime or not, using a user-defined function in C# programming language with Example program
Prime number
The number that can be divided by 1 and itself. it is called a prime number
for Example 2,3,5,7,11,13…
C# example to check whether a Number is Prime or Not
Check the prime number using a function– method 1
This program allows you to enter the input of a positive number and then it will check whether the given number is a prime number or not, using the function in C# language
Program 1
using System; public class CheckPrime { public static bool Check_Prime(int num){ for (int i=2; i<num; i++) if(num%i==0) return false; return true; } public static void Main(string[] args) { Console.Write ("Input a number: "); int n=Convert.ToInt32(Console.ReadLine()); if(Check_Prime(n)) Console.WriteLine (n+" is a prime"); else Console.WriteLine (n+" is not a prime"); } }
When the above code is executed, it produces the following result
Case 1
Input a number: 13 13 is a prime
Case 2
Input a number: 20 20 is not a prime
Check the prime number using a function– method 2
This program allows one to enter input of a positive number and then it will check whether the given number is a prime number or not, using the function in C# language
Program 2
//function to check a number prime or not using System; public class CheckPrime { public static int Check_Prime(int number){//uer define function int i; for (i=2; i<=number-1; i++){ if(number%i==0){ return 0; } } if(i==number){ return 1; } return 0; } public static void Main(string[] args) { Console.Write ("Input a number: "); int number=Convert.ToInt32(Console.ReadLine()); int res= Check_Prime(number); if(res==0){ Console.WriteLine ("{0} is not a prime number",number);} else{ Console.WriteLine ("{0} is a prime number",number);} } }
When the above code is executed, it produces the following result
Case 1
Input a number: 17 17 is a prime number
Case 2
Input a number: 22 22 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