Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
PHP| Check if a number is prime or not

Write a C# program: function to check whether a number is prime or not

Posted on December 11, 2022

Table of Contents

  • Write a C# program: function to check whether a number is prime or not
    • Code to check prime number
      • Check the prime number in C# using function   – #1
      • Check the prime number in JS using function – #2

Write a C# program: function to check whether a number is prime or not

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

Code to check prime number

Check the prime number in C# using function   – #1

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

 

Check the prime number in JS using function – #2

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

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes