Thursday, 12 February 2015

Get Prime Numbers in C#

Q. 1 What is Prime Number?
Answer :  A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 
Q. 2 Write a programmer in C# to check number is prime or not ?
Answer : The following code snippet  to check prime number or not.
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            int number = Convert.ToInt32(Console.ReadLine());
            int result = Check_Prime(number);
            if (result == 0)
            {
                Console.WriteLine("{0} is not a prime number"number);
            }
            else
            {
                Console.WriteLine("{0} is  a prime number"number);
            }
            Console.Read();
        }
        private static int Check_Prime(int number)
        {
            int i;
            for (i = 2i <= number - 1i++)
            {
                if (number % i == 0)
                {
                    return 0;
                }
            }
            if (i == number)
            {
                return 1;
            }
            return 0;
        }
    }

Another Program is :

class Program
    {
      static void Main(string[] args)
      {
  Console.WriteLine("Please enter the number in which you want to find the prime no");
            int a = Convert.ToInt32(Console.ReadLine());
            bool isPrime = true;

            for (int i = 1i <= ai++)
            {
                for (int j = 2j <= aj++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }

                if (isPrime)
                {
                    Console.WriteLine("Prime:" + i);
                }
                isPrime = true;
            }
            Console.ReadLine();
        }
    }



Another Program is :   
class Math
    {
        static bool IsPrimeNumber(int num)
        {
            bool bPrime = true;
            int factor = num / 2;

            for (int i = 2i <= factori++)
            {
                if ((num % i) == 0)
                    bPrime = false;
            }
            return bPrime;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("List of prime numbers between 0 - 15");
            for (int i = 0i < 15i++)
            {
                if (IsPrimeNumber(i) == true)
                    Console.WriteLine(i);
            }
            Console.WriteLine();
            Console.Read();
        }
    }




0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives