Check value

C# program to check if a number is prime or not

C# program to check if a number is prime or not

In this tutorial, we will discuss the concept of the C# program to check whether a Number is Prime or Not

In this post, we are going to learn how to check whether a number is prime or not  in Csharp programming language with Example program

 

Prime number

The number that can be divided by 1 and itself ony. it is called a prime number

for Example 2,3,5,7,11,13…

Check prime or not

Example to check whether a Number is Prime or Not

Check the prime number – variable initialization

In this program, we will initialize the variable as a positive number, then it will check whether the given number is a prime number or not, using the for loop in C# 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=7,count=0;
        for (int i=1; i<=num; i++){
            if(num%i==0){
                count++;
            }
            }
        if(count==2){
        Console.WriteLine ("{0} is a prime number",num);
    }else{
    Console.WriteLine ("{0} is  noy a prime number",num);
}
}
}

When the above code is executed, it produces the following result

7 is a prime number

 

Check the prime number –  Entered by the user

This program allows the user to enter a positive number then it will check whether the given number is a prime number or not, using the for loop in C# 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;
         Console.WriteLine ("Enter the number to check prime or not");
         num=int.Parse( Console.ReadLine());
         int m=num/2;
        for (i=2; i<=m; i++){
            if(num%i==0){
               Console.WriteLine ("{0} is not a prime number",num);
               count=1;
               break;
            }
            }
        if(count==0){
    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
17
17 is a prime number

 

Case 2

Enter the number to check prime or not
10
10 is not a prime number

 

 

 

Similar post

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

 

 

C++ program: find first n prime numbers
C# function to check a number is prime or not
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago