ejercicios csharp

10
/* * 1. Elaborar un programa quer calcule la edad exacta en años de una persona introduciendo * únicamente la fecha de nacimiento. Ejemplo: si la fecha de nacimiento es 15/04/1975 y * la fecha actual es 10/03/2003, entonces la edad es 27 años. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ejercicio1 { class Program { static void Main(string[] args) { DateTime FA = DateTime.Now; DateTime FN; int Edad; Console.WriteLine("Fecha actual: {0}\n", FA); Console.Write("Ingrese su fecha de nacimiento dd/mm/aaaa: "); FN = DateTime.Parse(Console.ReadLine()); Edad = FA.Year - FN.Year; if (FA.Month < FN.Month || (FA.Month == FN.Month && FA.Day < FN.Day)) Edad -= 1; Console.WriteLine("Su edad es: {0} años.", Edad); Console.ReadKey(); } } } /* * 2. Crear un programa que permita introducir N cantidad de datos enteros en una matriz * (n Filas y n Columnas) y muestre al final cuantos números son primos y cuantos son * pares. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ejercicio2 { class Program { static void Main(string[] args) { int N = 0, NPrimos = 0, NPares = 0;

Upload: david-castillo

Post on 28-Mar-2015

438 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Ejercicios CSharp

/* * 1. Elaborar un programa quer calcule la edad exacta en años de una persona introduciendo * únicamente la fecha de nacimiento. Ejemplo: si la fecha de nacimiento es 15/04/1975 y * la fecha actual es 10/03/2003, entonces la edad es 27 años. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio1{ class Program { static void Main(string[] args) { DateTime FA = DateTime.Now; DateTime FN; int Edad;

Console.WriteLine("Fecha actual: {0}\n", FA); Console.Write("Ingrese su fecha de nacimiento dd/mm/aaaa: "); FN = DateTime.Parse(Console.ReadLine());

Edad = FA.Year - FN.Year;

if (FA.Month < FN.Month || (FA.Month == FN.Month && FA.Day < FN.Day)) Edad -= 1;

Console.WriteLine("Su edad es: {0} años.", Edad);

Console.ReadKey(); } }}

/* * 2. Crear un programa que permita introducir N cantidad de datos enteros en una matriz * (n Filas y n Columnas) y muestre al final cuantos números son primos y cuantos son * pares. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio2{ class Program { static void Main(string[] args) { int N = 0, NPrimos = 0, NPares = 0; int[ , ] Numeros; bool EsPrimo;

Console.Write("Ingrese el tamaño del arreglo: "); N = int.Parse(Console.ReadLine()); Numeros = new int[N , N];

for (int i = 0; i <= Numeros.GetUpperBound(0); i++) { for (int j = 0; j <= Numeros.GetUpperBound(1); j++)

Page 2: Ejercicios CSharp

{ EsPrimo = true; Console.Write("Ingrese el valor a la pocisión [{0},{1}]: ", i, j); Numeros[i, j] = int.Parse(Console.ReadLine());

for (int k = Numeros[i, j] - 1; k > 1; k--) { if (Numeros[i, j] % k == 0) { EsPrimo = false; break; } }

if (EsPrimo == true) NPrimos += 1; if (Numeros[i, j] % 2 == 0) NPares += 1; } }

Console.WriteLine("\nCantidad de números primos: {0}", NPrimos); Console.WriteLine("Cantidad de números pares: {0}", NPares);

Console.ReadKey(); } }}

/* * 3. Elaborar un programa que permita introducir N datos numéricos (n filas) y calcule la * media de números introducidos, el número mayor y el número menor. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio3{ class Program { static void Main(string[] args) { decimal Media = 0; int N = 0, NMayor = 0, NMenor = 0, SumaN = 0; int[] Numeros;

Console.Write("Ingrese el tamaño del arreglo: "); N = int.Parse(Console.ReadLine()); Numeros = new int[N];

Console.Clear();

for (int i = 0; i < Numeros.Length; i++) { Console.Write("Ingrese el valor a la posición {0}: ", i); Numeros[i] = int.Parse(Console.ReadLine());

if (i == 0) NMenor = Numeros[i]; if (Numeros[i] < NMenor) NMenor = Numeros[i]; if (Numeros[i] > NMayor) NMayor = Numeros[i];

SumaN += Numeros[i]; }

Page 3: Ejercicios CSharp

Media = (decimal) SumaN / Numeros.Length;

Console.WriteLine("El mayor valor es: {0}", NMayor); Console.WriteLine("El menor valor es: {0}", NMenor); Console.WriteLine("La media es: {0}", Media);

Console.ReadKey(); } }}

/* * 6. Elaborar un programa que capture un rango de años y determine cuales son bisiestos. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio6{ class Program { static void Main(string[] args) { int PA = 0, SA = 0;

Console.Write("Ingrese desde que año desea calcular: "); PA = int.Parse(Console.ReadLine());

Console.Write("Ingrese hasta que año desea calcular: "); SA = int.Parse(Console.ReadLine());

for (int i = PA; i <= SA; i++) { if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) Console.WriteLine("El año {0} es bisiesto.", i); else Console.WriteLine("El año {0} no es bisiesto.", i); }

Console.ReadKey(); } }}

/* * 7. Elaborar un programa que permita introducir en una matriz bidimensional N cantidad de * datos numéricos (m Filas y n Columnas) y determine la posición del número menor. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio7{ class Program { static void Main(string[] args) { int n = 0, m = 0, NMenor = 0, F = 0, C = 0;

Page 4: Ejercicios CSharp

int[,] Numeros;

Console.Write("Ingrese la cantidad de filas: "); n = int.Parse(Console.ReadLine());

Console.Write("Ingrese la cantidad de columnas: "); m = int.Parse(Console.ReadLine());

Console.Clear();

Numeros = new int[n, m];

for (int i = 0; i <= Numeros.GetUpperBound(0); i++) { for (int j = 0; j <= Numeros.GetUpperBound(1); j++) { Console.Write("Ingrese el valor a la posición [{0},{1}]: ", i, j); Numeros[i, j] = int.Parse(Console.ReadLine());

if (i == 0) { NMenor = Numeros[i, j]; F = i; C = j; }

if (Numeros[i, j] < NMenor) { NMenor = Numeros[i, j]; F = i; C = j; } } }

Console.WriteLine("\nEl numero menor se encuentra en la fila {0} en la columna {1}.", F, C);

Console.ReadKey(); } }}

/* * 8. Elaborar un programa que permita introducir un número y calcule el factorial del mismo. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio8{ class Program { static void Main(string[] args) { int N = 0, Fact = 1;

Console.Write("Ingrese un número: "); N = int.Parse(Console.ReadLine());

for (int i = N; i > 0; i--) {

Page 5: Ejercicios CSharp

Fact *= i; }

Console.WriteLine("El factorial de {0} es: {1}", N, Fact);

Console.ReadKey(); } }}

/* * 9. Elaborar un programa que permita introducir dos números y 4 opciones para determinar * la suma, la resta, la multiplicación y la división entre dichos números. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio9{ class Program { static void Main(string[] args) { int Num1 = 0, Num2 = 0; char Operacion = ' ';

Console.Write("Ingrese el 1er número: "); Num1 = int.Parse(Console.ReadLine());

Console.Write("Ingrese el 2do número: "); Num2 = int.Parse(Console.ReadLine());

Console.WriteLine("\nDigite la operación a realizar: "); Console.WriteLine("'+' para sumar\n'-' para restar\n'*' para multiplicar\n'/' para dividir");

Console.Write("Digite la operacion: "); Operacion = char.Parse(Console.ReadLine());

switch(Operacion) { case '+': Console.WriteLine("\nResultado es: {0}", Num1 + Num2); break; case '-': Console.WriteLine("\nResultado es: {0}", Num1 - Num2); break; case '*': Console.WriteLine("\nResultado es: {0}", Num1 * Num2); break; case '/': if (Num2 != 0) Console.WriteLine("\nResultado es: {0}", (double)Num1 / Num2); else Console.WriteLine("\nValor no existe."); break; default: Console.WriteLine("\nOpción invalida"); break; }

Page 6: Ejercicios CSharp

Console.ReadKey(); } }}

/* * 10. Elaborar un programa que permita mostrar la tabla de multiplicar en base a un número * introducido. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio10{ class Program { static void Main(string[] args) { int N = 0;

Console.Write("Ingrese un número: "); N = int.Parse(Console.ReadLine());

Console.Clear();

for (int i = 1; i <= 10; i++) { Console.WriteLine("{0} x {1} = {2}", N, i, i * N); }

Console.ReadKey(); } }}

/* * 11. Elaborar un programa que determine cual es la hipotenusa de un triangulo rectángulo * en base a sus dos catetos conocidos. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio11{ class Program { static void Main(string[] args) { double CatetoAd = 0, CatetoOp = 0, Hipotenusa = 0;

Console.Write("Ingrese el valor del Cateto Adyacente: "); CatetoAd = double.Parse(Console.ReadLine());

Console.Write("Ingrese el valor del cateto opuesto: "); CatetoOp = double.Parse(Console.ReadLine());

Hipotenusa = Math.Sqrt(Math.Pow(CatetoAd, 2) + Math.Pow(CatetoOp, 2));

Console.WriteLine("El valor de la hipotenusa es: {0}", Hipotenusa);

Page 7: Ejercicios CSharp

Console.ReadKey(); } }}

/* * 12. Elaborar un programa que permita introducir un frase u oración y determine cuantas * letras tiene dicha frase sin tomar en cuenta los espacios, los números y los * caracteres especiales. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio12{ class Program { static void Main(string[] args) { string frase, letra; int letras = 0;

Console.Write("Ingrese una frase u oración: "); frase = Console.ReadLine();

for (int i = 0; i < frase.Length; i++) { letra = frase.Substring(i, 1); byte Caracter = ASCIIEncoding.ASCII.GetBytes(frase)[0];

if ((Caracter >= 65 && Caracter <= 90) || (Caracter >= 97 && Caracter <= 122) || (Caracter >= 160 && Caracter <= 165 || Caracter == 130)) { if (letra != " ") letras += 1; } } Console.WriteLine("Cantidad de letras: {0}", letras);

Console.ReadKey(); } }

/* * 15. Elaborar un programa que determine si una palabra o frase introducida es palindrome. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio15{ class Program { static void Main(string[] args) { string palabra = "", palindrome = ""; bool resp;

Page 8: Ejercicios CSharp

Console.Write("Ingrese una palabra: "); palabra = Console.ReadLine();

for (int i = palabra.Length - 1; i >= 0; i--) { palindrome += palabra.Substring(i, 1); }

resp = palabra == palindrome ? true : false;

if (resp == true) Console.WriteLine("La palabra es palindrome"); else Console.WriteLine("La palabra no es palindrome");

Console.ReadKey(); } }}

/* * 19. Crear un programa que llene de ceros "0" las diagonales principales de una matriz * cuadrada y llene con unos "1" las posiciones restantes. */

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Ejercicio19{ class Program { static void Main(string[] args) { int N = 0; int[,] Cuadrado;

Console.Write("Ingrese el tamaño de la matriz: "); N = int.Parse(Console.ReadLine()); Cuadrado = new int[N, N];

Console.Clear();

for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if((i == j) || ((j + i) == (N - 1))) Console.Write("0 "); else Console.Write("1 "); } Console.WriteLine(); }

Console.ReadKey(); } }}