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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago