Advertisement
ntamas

n-edik prím tömbfeltöltéssel

Oct 21st, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace nedikprimtomb
  7. {
  8.   class Program
  9.   {
  10.     static bool isprime(int szam)
  11.     {
  12.       if (szam < 2)
  13.       {
  14.         return false;
  15.       }
  16.       else if (szam == 2)
  17.       {
  18.         return true;
  19.       }
  20.       else
  21.       {
  22.         bool prim = true;
  23.         for(int i = 2; i< szam; i++)
  24.         {
  25.           if(szam % i == 0)
  26.           {
  27.             prim = false;
  28.             break; // Akkor juszt is breaket használok. :)
  29.           }
  30.         }
  31.         return prim;
  32.       }
  33.     }
  34.     static void Main(string[] args)
  35.     {
  36.       int b = 1, tal = 0;
  37.       uint elemszam;
  38.       string a;
  39.       Console.Write("Adja meg hogy az első hány db prímet szeretné kiíratni: ");
  40.       a = Console.ReadLine();
  41.       elemszam = Convert.ToUInt32(a);
  42.       int[] tomb = new int[elemszam];
  43.       do
  44.       {
  45.         if(isprime(b))
  46.         {
  47.           tomb[tal++] = b;
  48.         }
  49.         b++;
  50.       }while (tal < elemszam);
  51.       for (int j = 0; j < elemszam; j++)
  52.       {
  53.         Console.WriteLine("A {0}. elem = {1}", j+1, tomb[j]);
  54.       }
  55.       Console.ReadKey();
  56.     }
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement