Advertisement
Fhernd

UnionConjuntos.cs

Nov 6th, 2017
10,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace cap07.colecciones
  5. {
  6.     public class UnionConjuntos
  7.     {
  8.         public static void Main()
  9.         {
  10.             HashSet<int> pares = new HashSet<int>();
  11.             HashSet<int> impares = new HashSet<int>();
  12.            
  13.             for(int i = 1; i <= 10; ++i)
  14.             {
  15.                 if(i % 2 == 0 ){
  16.                     pares.Add(i);
  17.                 } else {
  18.                     impares.Add(i);
  19.                 }
  20.             }
  21.            
  22.             Console.WriteLine("Contenido conjunto números pares:");
  23.             MostrarConjunto(pares);
  24.            
  25.             Console.WriteLine("\nContenido conjunto números impares:");
  26.             MostrarConjunto(impares);
  27.            
  28.             HashSet<int> numerosEnteros = new HashSet<int>(pares);
  29.             numerosEnteros.UnionWith(impares);
  30.            
  31.             Console.WriteLine("\nConjunto resultante de la unión de pares e impares:");
  32.             MostrarConjunto(numerosEnteros);
  33.         }
  34.        
  35.         public static void MostrarConjunto(HashSet<int> conjunto)
  36.         {
  37.             foreach(int numero in conjunto)
  38.             {
  39.                 Console.Write("{0} ", numero);
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement