Check Prime or not
Table of Contents
In this article, we will discuss the concept of the Write a C# program: function 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 using the function, and display the result on the screen in JS language
In this program, the user is asked to enter values to variables “num”, and the program checks whether the number is prime or not in C#.
Program 1
//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: 31 31 is a prime number
Case 2
Input a number: 35 35 is not a prime number
In this program, the user is asked to enter values to variables “num”, and the program checks whether the number is prime or not in C# using the function.
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: ");
//ask input from the user
int number=Convert.ToInt32(Console.ReadLine());
//Reading the input
int res= Check_Prime(number);
//Calling the function and assign the result to var res
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: 61 61 is a prime number
Case 2
Input a number: 72 72 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.