Q:

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

0

Write a program in C# Sharp to create a function to check whether a number is prime or not. 
Test Data :
Input a number : 31
Expected Output :
31 is a prime number

All Answers

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

using System;
public class funcexer9
{
    public static bool chkprime(int num)
    {
        for (int i=2; i < num; i++)
          if (num %i == 0) 
            return false;
        return true;
    }
    public static void Main()
    {
	  Console.Write("\n\nFunction : To check a number is prime or not :\n");
      Console.Write("--------------------------------------------------\n");
	  Console.Write("Input a number : ");
      int n= Convert.ToInt32(Console.ReadLine());	
	
        if (chkprime(n))
          Console.WriteLine(n+" is a prime number");
        else
          Console.WriteLine(n+" is not a prime number");
    }
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now