Thursday, 12 February 2015

Coding Test: Prime Numbers in C#

What is a prime number?
A prime number is a number that can only be divided by itself and 1 without remainders.
What are the prime numbers from 1 to 100?
The prime numbers from 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.
Why is 1 not a prime number?
1 is not a prime number because it has only one factor, namely 1. Prime numbers need to have exactly two factors.
Why is 2 a prime number?
2 is a prime number because its only factors are 1 and itself.
Is 51 a prime number?
51 is not a prime number because it has 3 and 17 as divisors, as well as itself and 1. In other words, 51 has four factors.

Below is a simple C# program to check if a number is prime.

Prime Number Program in C#

using System;
class Program
{
    static void Main()
    {
        // Get the number from the user
        Console.Write("Enter a number: ");
        int number = Convert.ToInt32(Console.ReadLine());

        // Check if the number is prime
        if (IsPrime(number))
        {
            Console.WriteLine($"{number} is a prime number.");
        }
        else
        {
            Console.WriteLine($"{number} is not a prime number.");
        }
    }

    // Method to check if a number is prime
    static bool IsPrime(int num)
    {
        if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
        if (num == 2) return true;  // 2 is a prime number

        // Check divisibility from 2 to the square root of the number
        for (int i = 2; i <= Math.Sqrt(num); i++)
        {
            if (num % i == 0)
                return false; // If divisible by any number, it's not prime
        }

        return true; // The number is prime
    }
}

Write code to find prime numbers from 1 to 100 
using System;
class Program
static void Main(string[] args)
  {
      for (int i = 1; i <= 100; i++)
      {
          // Check if the number is prime
          if (IsPrime(i))
          {
              Console.WriteLine(i);
          }                
      }
  }
// Method to check if a number is prime
    static bool IsPrime(int num)
    {
        if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
        if (num == 2) return true;  // 2 is a prime number

        // Check divisibility from 2 to the square root of the number
        for (int i = 2; i <= Math.Sqrt(num); i++)
        {
            if (num % i == 0)
                return false; // If divisible by any number, it's not prime
        }

        return true; // The number is prime
    }
}
Output:


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# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) 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