prime or not

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

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

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.