What is Prime Number ?

A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //Prime Number : A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
  7. namespace PrimeNumber
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int number, primeNumber;
  14. Console.Write("\n\nInput any positive number : ");
  15. number = Convert.ToInt32(Console.ReadLine());
  16. Console.WriteLine("\n\n Recursion : Check a number is prime number or not :");
  17. Console.WriteLine("--------------------------------------------------------");
  18. primeNumber = IsPrime(number, number / 2);
  19. if (primeNumber == 1)
  20. Console.Write("\n\nThe number {0} is a prime number. \n\n", number);
  21. else
  22. Console.WriteLine("\n\nThe number {0} is not a prime number. \n\n", number);
  23. Console.WriteLine("--------------------------------------------------------");
  24. Console.WriteLine("\n\n Non-Recursion : Check a number is prime number or not :");
  25. Console.WriteLine("--------------------------------------------------------");
  26. //Without Recursion
  27. int i, flag = 0;
  28. for (i = 2; i <= number / 2; ++i)
  29. {
  30. if (number % i == 0)
  31. {
  32. flag = 1;
  33. break;
  34. }
  35. }
  36. if (flag == 0)
  37. Console.Write("\n\nThe number {0} is a prime number. \n\n", number);
  38. else
  39. Console.WriteLine("\n\nThe number {0} is not a prime number. \n\n", number);
  40. Console.WriteLine("--------------------------------------------------------");
  41. Console.ReadKey();
  42. }
  43. //Using Recursion
  44. static int IsPrime(int number, int i)
  45. {
  46. if (i == 1)
  47. {
  48. return 1;
  49. }
  50. else
  51. {
  52. if (number % i == 0)
  53. return 0;
  54. else
  55. return IsPrime(number, i - 1);//calling the function IsPrime itself recursively
  56. }
  57. }
  58. }
  59. }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


//Prime Number : A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {

            int number, primeNumber;           

            Console.Write("\n\nInput any positive number : ");
            number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\n\n Recursion : Check a number is prime number or not :");
            Console.WriteLine("--------------------------------------------------------");

            primeNumber = IsPrime(number, number / 2);

            if (primeNumber == 1)
                Console.Write("\n\nThe number {0} is a prime number. \n\n", number);
                else
                Console.WriteLine("\n\nThe number {0} is not a prime number. \n\n", number);

            Console.WriteLine("--------------------------------------------------------");
            Console.WriteLine("\n\n Non-Recursion : Check a number is prime number or not :");
            Console.WriteLine("--------------------------------------------------------");

            //Without Recursion
            int i, flag = 0;

            for (i = 2; i <= number / 2; ++i)
            {

                if (number % i == 0)
                {

                    flag = 1;
                    break;

                }

            }

            if (flag == 0)
                Console.Write("\n\nThe number {0} is a prime number. \n\n", number);
            else
                Console.WriteLine("\n\nThe number {0} is not a prime number. \n\n", number);

            Console.WriteLine("--------------------------------------------------------");
            Console.ReadKey();

        }

        //Using Recursion 
        static int IsPrime(int number, int i)
        {

            if (i == 1)
            {

                return 1;

            }
            else
            {

                if (number % i == 0)
                    return 0;
                else
                    return IsPrime(number, i - 1);//calling the function IsPrime itself recursively
            }

        }
    }
}

You can also download the code from our GitHub.

Happy Coding. 🙂