Q:

Write a C# program to create a function to check whether a number is prime or not

0

Write a C# program to create a function to check whether a number is prime or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
 
public class functionexcercise
{
    public static bool checkprime(int num)
    {
        for (int i=2; i < num; i++)
          if (num %i == 0) 
            return false;
        return true;
    }
    public static void Main()
    {      
      Console.Write("Enter a number : ");
      int n= Convert.ToInt32(Console.ReadLine());
 
      if (checkprime(n))
          Console.WriteLine(n+" is a prime number");
        else
          Console.WriteLine(n+" is not a prime number");
 
      Console.ReadLine();
    }
}

Result:

Enter a number : 167

167 is a prime number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C# program to create a function to swap th... >>
<< Write a C# program to create a function to calcula...