0
write a program to check a number is prime or not?
3 Respostas
0
Other than 2 all prime numbers are odd. The code above checks for odd or even if it is odd then the program must check all odd numbers below it for a remander. So 47 must be checked against 3,7,11... And so on i left out 5,9 because it is quick to check if a number is divisable by 5 it would end in a 5 or 0. And 9 is divisable by 3 which was already checked. If i had a quicker way id be rich. Lol look up prime number prize.
0
All we need to do is to check is the number is divisible by any of the numbers greater than 1 and lower than its square root:
int n;
double sqrRoot;
bool isPrime = true;
n = Int.Parse(Console.ReadLine());
sqrRoot = Math.Sqrt(n);
for (int i = 2; i < sqrRoot; i++)
{
if (n % i == 0) isPrime = false;
}
if (isPrime) Console. WriteLine("{0} is a prime.", n);
else Console.WriteLine("{0} is not a prime.", n);
- 1
a prime number is one tht can be divider by 1 and itself.
so u can write a logic so tht the number u entered will be checked to these condition.
you can use thi for example:
for (int i=2; i <=number; number ++){
if (number%i ==0){
Console.WriteLine ("Number is a prime");
}
}
something like this..you can always improve