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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. public class functionexcercise
  4. {
  5. public static bool checkprime(int num)
  6. {
  7. for (int i=2; i < num; i++)
  8. if (num %i == 0)
  9. return false;
  10. return true;
  11. }
  12. public static void Main()
  13. {
  14. Console.Write("Enter a number : ");
  15. int n= Convert.ToInt32(Console.ReadLine());
  16.  
  17. if (checkprime(n))
  18. Console.WriteLine(n+" is a prime number");
  19. else
  20. Console.WriteLine(n+" is not a prime number");
  21.  
  22. Console.ReadLine();
  23. }
  24. }

Result

C# language techstudy

     





Related Topics




Popular Posts