Check Whether Number is a Prime number, if so, Display Largest Factor

prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number.

For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 6 is composite because it is the product of two numbers (2 × 3) that are both smaller than 6. Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order.

 

using System;

namespace Program
{
    class Program
    {
        public static void Main()
        {
            int num;
            int k;
            k = 0;

            Console.Write("Enter a Number: ");            
            num = Convert.ToInt32(Console.ReadLine());
            
            for (int i = 1; i <= num; i++)
            {
                if (num % i == 0)
                {
                    k++;
                }
            }

            if (k == 2)
            {
                Console.WriteLine("Entered Number is a Prime Number and the Largest Factor is {0}", num);
            }
            else
            {
                Console.WriteLine("Not Prime!");
            }

            Console.ReadLine();
        }
    }
}

 

Output