Skip to main content

How to check a number Prime or not in c# ?

/*Check a number Prime or not in C# */

Program :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Primenumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, flag = 0;
            Console.WriteLine("Enter A number :");
            num = Convert.ToInt32(Console.ReadLine());
            if (num > 0)
            {
                for (int i = 2; i < num - 1; i++)
                {
                    if (num % i == 0)
                    {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    Console.WriteLine("The number " + num + " is a Prime number .");
                else
                    Console.WriteLine("The number " + num + " is not a Prime number .");
            }
            else
                Console.WriteLine("Enter A valid Number :\n");

            Console.ReadKey();

        }
    }
}

Output :

Enter A number :
5
The number 5 is a Prime number .
           
             
Enter A number :
6
The number 6 is not a Prime number .
            

Comments

Popular posts from this blog

How to get Reverse Of a number in C# ?

Reverse of a positive integer integer  number in c#  Program : using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Reverse {     class Program     {         static void Main( string [] args)         {             int num, rem, rev = 0;             Console .WriteLine( "//How to get Reverse of a number in c# ?" );             Console .WriteLine( "Enter A number :" );             num = Convert .ToInt32( Console .ReadLine());             if (num > 0)     ...

How to Copy one datatable to another datatable

Introduction : In this article i am going to share about how to copy a datatable to a new datatable object , and how to add a new row to the datatable. Overview : Generally we used datatable object to store out table data, sometime we want to store that datatable temporally to a another datatable object that time we need to copy the datatable and store to newly created object . It's a best practice to copy our main datatable when we need that datatable multiple times  and used the copyed object. We can add a new column dynamically to the newly created object. Code Example : Syntex : DataTable dest = MainDatatable; Connection String : <connectionStrings>    <add name="conn" connectionString="Data Source=anil\SQLEXPRESS2012;Initial Catalog=MyDB; User ID=sa;Password=sa1" providerName="System.Data.SqlClient" />  </connectionStrings> C# Code : SqlConnection con = new SqlConnec...