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 to check whether a number is prime or not

Posted on December 10, 2022January 22, 2023

Table of Contents

  • Write a C# program to check whether a number is prime or not
    • Code to check prime number
      • Check the prime number in C#
      • Check the prime number in C#-Entered by the user
      • Check the prime number in C#- using the function

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

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.

Code to check prime number

Check the prime number in C#

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

 

Check the prime number in C#-Entered by the user

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

 

Check the prime number in C#- using the function

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

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