Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write an expression that checks if given positive integer number n (n ≤ 100) is prime (i.e. it is divisible without remainder only to itself and 1). Examples:
- //n Prime?
- //1 false
- //2 true
- //3 true
- //4 false
- //9 false
- //97 true
- //51 false
- //-3 false
- //0 false
- class PrimeNumberCheck
- {
- static void Main()
- {
- Console.Write("Enter a number (n <= 100): ");
- int userInput = int.Parse(Console.ReadLine());
- int upperLimit = (int)Math.Sqrt(userInput);
- bool isPrime = true;
- if (userInput > 0 && userInput != 1) {
- for (int i = 2; i < upperLimit; i++)
- {
- if (userInput % i == 0)
- {
- isPrime = false;
- break;
- }
- else
- {
- isPrime = true;
- }
- }
- }
- else if (userInput == 1)
- {
- isPrime = false;
- }
- else
- {
- isPrime = false;
- }
- Console.WriteLine(isPrime == true ? true : false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement