Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace PrimeNumber
- {
- class PrimeNumber
- {
- static void Main()
- {
- int number;
- Console.WriteLine("Enter a random number to check if it's prime:");
- number = Convert.ToInt32(Console.ReadLine());
- int forCaseSt;
- Console.WriteLine("Choose an algorithm for checking the number (1 for SQRT, 2 for HALF).");
- forCaseSt = Convert.ToInt32(Console.ReadLine());
- if (forCaseSt == 1)
- {
- double numberSqrt = Math.Sqrt(number);
- int numberSqrtRound = (int)Math.Round(numberSqrt);
- if (number == 0 || number == 1)
- {
- Console.WriteLine(number + " is not a prime number");
- }
- else
- {
- for (int i = 2; i <= numberSqrtRound; i++)
- {
- if (number % i == 0)
- {
- Console.WriteLine(number + " is not a prime number.");
- return;
- }
- }
- Console.WriteLine(number + " is a prime number.");
- }
- }
- else if (forCaseSt == 2)
- {
- double numberHalf = (double)(number / 2);
- int numberHalfRound = (int)Math.Round(numberHalf);
- if (number == 0 || number == 1)
- {
- Console.WriteLine(number + " is not a prime number");
- }
- else
- {
- for (int i = 2; i <= numberHalfRound; i++)
- {
- if (number % i == 0)
- {
- Console.WriteLine(number + " is not a prime number");
- return;
- }
- }
- Console.WriteLine(number + " is a prime number");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement