coasterka

#UniTaskPrimeNums

Mar 25th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. //да се напише програма, която извежда простите числа от 1 до N (N се въвежда от клавиатурата)
  2. //Алгоритъм - числото X е просто ако няма число в интервала 2 до N/2, което e точен делител на X
  3. //2 e просто число
  4. //да се проверява, че потребителят е въвел цяло число , в противен случай да се въведе ново N
  5. //(0 за край без да се извежда съобщение за грешка)
  6.  
  7. internal static void Main(string[] args)
  8.         {
  9.             decimal endInterval = decimal.Parse(Console.ReadLine());
  10.  
  11.             while (!IsInt(endInterval))
  12.             {
  13.                 endInterval = decimal.Parse(Console.ReadLine());
  14.             }
  15.  
  16.             bool isPrime = true;
  17.  
  18.             for (int i = 2; i < endInterval; i++)
  19.             {
  20.                 for (int j = 2; j < endInterval / 2; j++)
  21.                 {
  22.                     if (i != j && i % j == 0)
  23.                     {
  24.                         isPrime = false;
  25.                         break;
  26.                     }
  27.                 }
  28.  
  29.                 if (isPrime)
  30.                 {
  31.                     Console.Write("{0} ", i);
  32.                 }
  33.  
  34.                 isPrime = true;
  35.             }
  36.  
  37.             Console.WriteLine();
  38.         }
  39.  
  40.         private static bool IsInt(decimal number)
  41.         {
  42.             return number % 1 == 0;
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment