Advertisement
ntamas

megoldás 1.

Jan 11th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Gyak1
  7. {
  8.   class Program //Két dimenziós tömb feltöltése, kiíratása
  9.   {
  10.     static bool isprime(int szam)
  11.     {
  12.       bool prim;
  13.       if (szam == 0 || szam == 1)
  14.       {
  15.         prim = false;
  16.       }
  17.       else if (szam == 2)
  18.       {
  19.         prim = true;
  20.       }
  21.       else
  22.       {
  23.         prim = true;
  24.         bool kilep = false;
  25.         for (int i = 2; i < Math.Sqrt(szam) && !kilep; i++)
  26.         {
  27.           if (szam % i == 0)
  28.           {
  29.             prim = false;
  30.             kilep = true;
  31.           }
  32.         }
  33.       }
  34.       return prim;
  35.     }
  36.     static bool paros(int szam)
  37.     {
  38.       bool par;
  39.       if (szam % 2 == 0)
  40.       {
  41.         par = true;
  42.       }
  43.       else
  44.       {
  45.         par = false;
  46.       }
  47.       return par;
  48.     }
  49.     static void keresparos(int[,] tomb)
  50.     {
  51.       for (int j = 0; j < tomb.GetLength(0); j++)
  52.       {
  53.         for (int i = 0; i < tomb.GetLength(1); i++)
  54.         {
  55.           if (paros(tomb[j, i]) == true)
  56.           {
  57.             Console.Write("{0} ", tomb[j, i]);
  58.           }
  59.         }
  60.       }
  61.     }
  62.     static void primkeres(int[,] tomb)
  63.     {
  64.       for (int j = 0; j < tomb.GetLength(0); j++)
  65.       {
  66.         for (int i = 0; i < tomb.GetLength(1); i++)
  67.         {
  68.           if (isprime(tomb[j, i]) == true)
  69.           {
  70.             Console.Write("{0} ", tomb[j, i]);
  71.           }
  72.         }
  73.       }
  74.     }
  75.     static void tombfeltolt(int[,] t, Random r)
  76.     {
  77.       for (int j = 0; j < t.GetLength(0); j++)
  78.       {
  79.         for (int i = 0; i < t.GetLength(1); i++)
  80.         {
  81.           t[j, i] = r.Next(10, 100);
  82.         }
  83.       }
  84.     }
  85.     static void kiir(int[,] tomb)
  86.     {
  87.       for (int j = 0; j < 5; j++)
  88.       {
  89.         for (int i = 0; i < 5; i++)
  90.         {
  91.           Console.Write("{0} ", tomb[j, i]);
  92.         }
  93.         Console.WriteLine();
  94.       }
  95.     }
  96.  
  97.     static void Main(string[] args)
  98.     {
  99.       int[,] tomb = new int[5, 5];
  100.       Random vel = new Random();
  101.       tombfeltolt(tomb, vel);
  102.       kiir(tomb);
  103.       Console.WriteLine();
  104.       keresparos(tomb);
  105.       Console.WriteLine();
  106.       primkeres(tomb);
  107.       Console.ReadKey();
  108.     }
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement