Tech Study

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

Introduction

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

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

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

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more