Answer the question
In order to leave comments, you need to log in
(C#) How does the for loop work in a specific example?
Help me understand this "simple example" from Schildt
// Простой пример применения параметра.
using System;
class ChkNum
{
// Возвратить значение true, если значение
// параметра х окажется простым числом.
public bool IsPrime(int x)
{
if (x <= 1) return false;
for (int i = 2; i <= x / i; i++)
if ((x % i) == 0) return false;
return true;
}
}
class ParmDemo
{
static void Main()
{
ChkNum ob = new ChkNum();
for (int i = 2; i < 10; i++)
if (ob.IsPrime(i)) Console.WriteLine(i + " простое число.");
else Console.WriteLine(i + " непростое число.");
}
}
Answer the question
In order to leave comments, you need to log in
Here is a test for prime numbers. A prime number is a natural number greater than 1 that has exactly two natural divisors: 1 and itself. We take 2. We look at the code.
for (int i = 2; i <= x / i; i++)
if ((x % i) == 0) return false;
return true;
For x=2:
if (x <= 1) return false;
for (int i = 2; i <= x / i; i++) // 2 > 1 - следующая строка не выолнится
if ((x % i) == 0) return false;
return true;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question