Check Prime or not
Table of Contents
In this article, we will discuss the concept of the Write a C# program to check whether a number is prime or not
In this post, we are going to learn how to write a program to check whether the given number is prime or not and display the result on the screen in C# programming language.
In this program, the user initiates the values to variables “num” as 17 and “count” as 0, and the program checks whether the given number(17) is prime or not in the C# programming 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=17,count=0;//variable declaration
for (int i=1; i<=num; i++){
if(num%i==0){
count++;//count increment by 1
}
}
if(count==2){//when the if statements is true
Console.WriteLine ("{0} is a prime number",num);
}else{//when the if statements is false
Console.WriteLine ("{0} is not a prime number",num);
}
}
} When the above code is executed, it produces the following result
17 is a prime number
In this program, the user is asked to enter values to variables “num” and the program checks whether the given number is prime or not using the C# programming 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;//variable declaration
Console.WriteLine ("Enter the number to check prime or not");
//Ask input from the user
num=int.Parse( Console.ReadLine());
//reading the input
int m=num/2;
for (i=2; i<=m; i++){
if(num%i==0){//when the if statement is true
Console.WriteLine ("{0} is not a prime number",num);
count=1;
break;
}
}
if(count==0){//when the if statement is true
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: 11 11 is a prime number
Case 2
Enter the number to check prime or not: 15 15 is not a prime number
In this program, the user is asked to enter values to variables and the program checks whether the given number is prime or not, using the function in the C# programming language.
Program 3
//function to check a number prime or not
using System;
public class CheckPrime
{
public static bool Check_Prime(int num){//uer define function
for (int i=2; i<num; i++)
if(num%i==0)
return false;
return true;
}
public static void Main(string[] args)//main
{
Console.Write ("Input a number: ");
int n=Convert.ToInt32(Console.ReadLine());
//Reading the input
if(Check_Prime(n))
Console.WriteLine ("{0} is a prime number",n);
else{
Console.WriteLine ("{0} is not a prime number",n);}
}
} When the above code is executed, it produces the following result
Case 1
Input a number: 23 23 is a prime number
Case 2
Input a number: 21 21 is not a prime number
Similar post
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Code to print prime numbers from 1 to 100 or 1 to n in Java
Code to print prime numbers from 1 to 100 or 1 to n in C
Code to print prime numbers from 1 to 100 or 1 to n in C++
Code to print prime numbers from 1 to 100 or 1 to n in Python
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.