Advertisement
btoth04

Prímszám vadász

May 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //értékek beállítása
  14.             Console.WriteLine("Maximum beírható érték: 2.147.483.647");
  15.             Console.Write("Alsó határ: ");
  16.             int min = Convert.ToInt32(Console.ReadLine());
  17.             if(min<2)
  18.             {
  19.                 Console.BackgroundColor = ConsoleColor.DarkRed;
  20.                 Console.ForegroundColor = ConsoleColor.White;
  21.                 Console.Clear();
  22.                 Console.WriteLine("!!!HIBA!!! A min értéke nem lehet kisebb 2-nél! Bezáráshoz nyomj bármilyen billentyűt!");
  23.                 Console.ReadKey();
  24.                 Environment.Exit(0);
  25.             }
  26.             Console.Write("Felső határ: ");
  27.             int max = Convert.ToInt32(Console.ReadLine());
  28.             if(max<=min)
  29.             {
  30.                 Console.BackgroundColor = ConsoleColor.DarkRed;
  31.                 Console.ForegroundColor = ConsoleColor.White;
  32.                 Console.Clear();
  33.                 Console.WriteLine("!!!HIBA!!! A max értéke nem lehet kisebb vagy egyenlő a min-nel! Bezáráshoz nyomj bármilyen billentyűt!");
  34.                 Console.ReadKey();
  35.                 Environment.Exit(0);
  36.             }
  37.             //prímszám vadászat
  38.             for (int n=min;n<=max;n++)
  39.             {
  40.                 int c = 0;
  41.                 for(int i=2;i<n;i++)
  42.                 {
  43.                     if(n%i==0)
  44.                     {
  45.                         c++;
  46.                     }
  47.                 }
  48.                 if(c==0)
  49.                 {
  50.                     Console.WriteLine(n);
  51.                 }
  52.             }
  53.             //vége
  54.             Console.BackgroundColor = ConsoleColor.DarkGreen;
  55.             Console.Write("A folyamat véget ért. Kilépéshez nyomd meg bármelyik billentyűt!");
  56.             Console.ReadKey();
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement