C# Program to check the square root of a number is prime or not


In this program, You will learn how to check the square root of a number is prime or not in C#.


49 Sqrt is :7 => Prime

Example: How to check the square root of a number is prime or not in C#.

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int n, i = 2, p = 1;
		double num;

		Console.Write("Enter a number:");
		num = Convert.ToDouble(Console.ReadLine());

		Console.Write("Square root is:" + Math.Sqrt(num));
    n = Convert.ToInt32(Math.Sqrt(num));


		while (i < n)
		{
			if (n % i == 0)
			{
				p = 0;
				break;
			}
			i++;
		}

		if (p == 1)
		{
			Console.WriteLine("\nNumber is prime:" + n);
		}
		else
		{
			Console.WriteLine("\nNumber is not prime:" + n);
		}
	}
}

Output:

Enter a number:49
Square root is:7
Number is prime:7