milardovich

Draft - Unidad 3 .NET UTN

Apr 6th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Unidad03
  7. {
  8.     class Program
  9.     {
  10.         //static int[] memo;
  11.         static void Main(string[] args)
  12.         {
  13.             Ejercicio9();
  14.             Console.ReadKey();
  15.         }
  16.  
  17.         static void Ejercicio1()
  18.         {
  19.             int nro1, nro2;
  20.             Console.WriteLine("Ingrese el primer número: ");
  21.             nro1 = Convert.ToInt32(Console.ReadLine());
  22.             Console.WriteLine("Ingrese el segundo número: ");
  23.             nro2 = Convert.ToInt32(Console.ReadLine());
  24.             Console.WriteLine("El resultado de la suma de "+nro1+" y "+nro2+" es "+(nro1+nro2));
  25.         }
  26.         static void Ejercicio2()
  27.         {
  28.             int anio;
  29.             Console.WriteLine("Ingrese el año: ");
  30.             anio = Convert.ToInt32(Console.ReadLine());
  31.             if((anio % 4 == 0 && anio % 100 != 0)  || (anio % 100 == 0 && anio % 400 == 0))
  32.             {
  33.                 Console.WriteLine("Es bisiesto");
  34.             } else {
  35.                 Console.WriteLine("No es bisiesto");
  36.             }
  37.         }
  38.         static void Ejercicio3()
  39.         {
  40.             Console.WriteLine("Diez: "+fib(10));
  41.         }
  42.         static int fib(int n)
  43.         {
  44.             /*
  45.              * Revisar tema de memo{}
  46.              */
  47.             int f;
  48.             if (n <= 2)
  49.                 f = 1;
  50.             else
  51.                 f = fib(n - 1) + fib(n - 2);
  52.             return f;
  53.         }
  54.         static void Ejercicio4()
  55.         {
  56.             int i;
  57.             for (i = 1; i <= 100; i++)
  58.             {
  59.                 if (i % 2 == 0)
  60.                 {
  61.                     Console.WriteLine(i);
  62.                 }
  63.             }
  64.         }
  65.         /*
  66.          *  ¿Existe algo como mes['enero'] = 1; ?
  67.          */
  68.         static void Ejercicio5()
  69.         {
  70.             String mes;
  71.             Boolean encontrado;
  72.             encontrado = false;
  73.             String[] meses = new String[13];
  74.             meses[1] = "Enero";
  75.             meses[2] = "Febrero";
  76.             meses[3] = "Marzo";
  77.             meses[4] = "Abril";
  78.             meses[5] = "Mayo";
  79.             meses[6] = "Junio";
  80.             meses[7] = "Julio";
  81.             meses[8] = "Agosto";
  82.             meses[9] = "Septiembre";
  83.             meses[10] = "Octubre";
  84.             meses[11] = "Noviembre";
  85.             meses[12] = "Diciembre";
  86.             Console.WriteLine("Ingrese el número del mes: ");
  87.             mes = Console.ReadLine();
  88.             for(int i=1;i<=12;i++)
  89.             {
  90.                 if(mes.ToLower() == meses[i].ToLower())
  91.                 {
  92.                     Console.WriteLine(meses[i]+" "+i);
  93.                     encontrado = true;
  94.                 }
  95.             }
  96.             if (!encontrado)
  97.             {
  98.                 Console.WriteLine("El mes no existe");
  99.             }
  100.            
  101.         }
  102.  
  103.         static void Ejercicio6()
  104.         {
  105.             int nro;
  106.             Console.WriteLine("Ingrese el número que desea convertir a romano: ");
  107.             nro = Convert.ToInt32(Console.ReadLine());
  108.             Console.WriteLine(ConvertirRomano(nro));
  109.         }
  110.         static string ConvertirRomano(int nro)
  111.         {
  112.             if ((nro < 0) || (nro > 3999)) throw new ArgumentOutOfRangeException("Debe insertar un valor entre 1 y 3999");
  113.             if (nro < 1) return string.Empty;
  114.             if (nro >= 1000) return "M" + ConvertirRomano(nro - 1000);
  115.             if (nro >= 900) return "CM" + ConvertirRomano(nro - 900);
  116.             if (nro >= 500) return "D" + ConvertirRomano(nro - 500);
  117.             if (nro >= 400) return "CD" + ConvertirRomano(nro - 400);
  118.             if (nro >= 100) return "C" + ConvertirRomano(nro - 100);
  119.             if (nro >= 90) return "XC" + ConvertirRomano(nro - 90);
  120.             if (nro >= 50) return "L" + ConvertirRomano(nro - 50);
  121.             if (nro >= 40) return "XL" + ConvertirRomano(nro - 40);
  122.             if (nro >= 10) return "X" + ConvertirRomano(nro - 10);
  123.             if (nro >= 9) return "IX" + ConvertirRomano(nro - 9);
  124.             if (nro >= 5) return "V" + ConvertirRomano(nro - 5);
  125.             if (nro >= 4) return "IV" + ConvertirRomano(nro - 4);
  126.             if (nro >= 1) return "I" + ConvertirRomano(nro - 1);
  127.             throw new ArgumentOutOfRangeException("error");
  128.         }
  129.  
  130.         static void Ejercicio7()
  131.         {
  132.             int n;
  133.             Console.WriteLine("Ingrese el máximo del intervalo: ");
  134.             n = Convert.ToInt32(Console.ReadLine())+1;
  135.             int[] primos = new int[n];
  136.             // Obtener todos los números primos desde 0 a n
  137.             for (int i = 1; i <= (n-1); i++)
  138.             {
  139.                 if (EsPrimo(i))
  140.                     primos[i] = 1;
  141.                 else
  142.                     primos[i] = 0;
  143.             }
  144.             // Comparamos los números encontrados
  145.             for (int k = 1; k <= (n-3); k++)
  146.             {
  147.                 if (primos[k] == 1 && primos[k + 2] == 1)
  148.                     Console.WriteLine("(" + k + "," + (k + 2) + ")");
  149.             }
  150.         }
  151.  
  152.         static Boolean EsPrimo(int n)
  153.         {
  154.             int a=0,i;  
  155.             for(i = 1; i < (n + 1); i++)
  156.             {  
  157.                 if(n%i == 0)
  158.                     a++;
  159.             }  
  160.             if(a != 2)  
  161.                 return false;
  162.             else
  163.                 return true;
  164.         }
  165.  
  166.         static void Ejercicio8()
  167.         {
  168.             int intentos = 0;
  169.             String pass = "abc";
  170.             String ingreso;
  171.             while(intentos < 4)
  172.             {
  173.                 Console.WriteLine("Ingrese la contraseña ");
  174.                 ingreso = Console.ReadLine();
  175.                 if(ingreso != pass)
  176.                 {
  177.                     Console.WriteLine("Contraseña Incorrecta.");
  178.                     intentos++;
  179.                 } else {
  180.                     Console.WriteLine("Contraseña Correcta.");
  181.                     break;
  182.                 }
  183.             }
  184.  
  185.         }
  186.  
  187.         static void Ejercicio9()
  188.         {
  189.             int filas,cant_fila;
  190.             cant_fila = 1;
  191.             Console.WriteLine("Ingrese la cantidad de filas: ");
  192.             filas = Convert.ToInt32(Console.ReadLine());
  193.             for (int i = 0; i < filas; i++)
  194.             {
  195.                 for (int j = 0; j < filas - i; j++)
  196.                 {
  197.                     Console.Write(" "); Console.Write(" ");
  198.                 }
  199.  
  200.                 for (int k = 0; k < cant_fila; k++)
  201.                 {
  202.                     Console.Write("* ");
  203.                 }
  204.                 Console.Write("\n");
  205.                 cant_fila += 2;
  206.             }
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment