Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.Write("Enter a number between 1-10 to find the prime number in that position: ");

            bool isValid = int.TryParse(Console.ReadLine(), out int userInput);

            while (!isValid)
            {
                Console.Write("That is not a valid entry. Please enter an integer between 1 and 10: ");
                isValid = int.TryParse(Console.ReadLine(), out userInput);
            }

            while (userInput > 10 || userInput < 1)
            {
                Console.Write("That is not a valid entry. Please enter an integer between 1 and 10: ");
                int.TryParse(Console.ReadLine(), out userInput);
            }

            Console.WriteLine($"The prime number at position {userInput} is {PrimeNumber.GetPrime(userInput)}");
        }
Ejemplo n.º 2
0
 private void btnExecute_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         bool canContinue = !string.IsNullOrEmpty(txtPrimeNumber.Text);
         if (canContinue)
         {
             int  numberToTest = Convert.ToInt32(txtPrimeNumber.Text);
             bool result       = PrimeNumber.isPrime(numberToTest);
             lblResult.Content = result ? $"{numberToTest} is a prime number" : $"{numberToTest} is not a prime number";
         }
         else
         {
             lblResult.Content = "Please input a number!!!";
         }
     }
     catch (Exception)
     {
         lblResult.Content = "An error has occurred. Please try again!";
     }
 }