Created
May 1, 2018 20:02
-
-
Save jorgesanabria/a494dd49c9ce69764257f62c7f294243 to your computer and use it in GitHub Desktop.
ejercicios lógica programación
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PracticasParcial1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
_ejercicios.Add(1, ImprimirRangoPares); | |
_ejercicios.Add(2, NumeroEsParImparNegativoPositivo); | |
_ejercicios.Add(3, NumeroPerfecto); | |
_ejercicios.Add(4, TablaMultiplicar); | |
_ejercicios.Add(5, CalcularCifras); | |
_ejercicios.Add(6, SucesionDeLucas); | |
_ejercicios.Add(7, DivisoresNaturales); | |
_ejercicios.Add(8, DivisiblesAlMismoTiempo); | |
Console.WriteLine("Practicas del parcial 1"); | |
Console.WriteLine("Ingrese el numero de ejercicio"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
if (!_ejercicios.ContainsKey(numero)) | |
{ | |
Console.WriteLine($"El ejerciocio numero {numero} no esta definido"); | |
Console.ReadKey(); | |
return; | |
} | |
_ejercicios[numero](); | |
Console.ReadKey(); | |
} | |
private static Dictionary<int, Action> _ejercicios = new Dictionary<int, Action>(); | |
private static void ImprimirRangoPares() | |
{ | |
Console.WriteLine("Ingrese un numero"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine("Ingrese otro numero, mayor al anterior"); | |
var numero2 = Convert.ToInt32(Console.ReadLine()); | |
var cantidadPares = 0; | |
for (var i = numero; i <= numero2; i++) | |
{ | |
if (i % 2 == 0 && i != 0) | |
{ | |
cantidadPares++; | |
Console.WriteLine($"numero {i} es par"); | |
} | |
} | |
Console.WriteLine($"La cantidad de numeros pares entre {numero} y {numero2} es {cantidadPares}"); | |
} | |
private static void NumeroEsParImparNegativoPositivo() | |
{ | |
Console.WriteLine("Ingrese un numero"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
if (numero % 2 == 0 && numero != 0) | |
{ | |
Console.WriteLine($"Numero {numero} es par"); | |
} | |
else if (numero == 0) | |
{ | |
Console.WriteLine($"Numero {numero} es neutro"); | |
} | |
else | |
{ | |
Console.WriteLine($"Numero {numero} es impar"); | |
} | |
if (numero >= 0) | |
{ | |
Console.WriteLine($"Numero {numero} es positivo"); | |
} | |
else | |
{ | |
Console.WriteLine($"Numero es negativo"); | |
} | |
} | |
private static void NumeroPerfecto() | |
{ | |
Console.WriteLine("Ingrese un numero para indicar si es perfecto o no"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
var suma = 0; | |
for (var i = 1; i < numero; i++) | |
{ | |
if (numero % i == 0) | |
{ | |
Console.WriteLine($"{i} es divisor entero de {numero}"); | |
suma = suma + i; | |
} | |
} | |
if (suma == numero) Console.WriteLine($"Numero {numero} es perfecto"); | |
else Console.WriteLine($"Numero {numero} no es perfecto"); | |
} | |
private static void TablaMultiplicar() | |
{ | |
Console.WriteLine("Ingrese un numero positivo para calcular su tabla de multiplicar"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
for (var i = 0; i <= 10; i++) | |
{ | |
Console.WriteLine($"{numero}x{i}={numero * i}"); | |
} | |
} | |
private static void CalcularCifras() | |
{ | |
Console.WriteLine("Ingrese un numero para calcular cuantas cifras tiene"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
var copia = numero; | |
var cantidadCifras = 0; | |
while(copia >= 0) | |
{ | |
copia = copia / 10; | |
cantidadCifras++; | |
if (copia == 0) break; | |
} | |
Console.WriteLine($"El numero {numero} tiene {cantidadCifras} cifras"); | |
} | |
private static void SucesionDeLucas() | |
{ | |
Console.WriteLine("Ingrese cuantos niveles de la sucesion de lucas desea mostrar"); | |
var nivelMaximo = Convert.ToInt32(Console.ReadLine()); | |
var a = 1; | |
var b = 3; | |
var auxiliar = 0; | |
for (var i = 0; i < nivelMaximo; i++) | |
{ | |
Console.WriteLine(a); | |
auxiliar = a; | |
a = b; | |
b = auxiliar + a; | |
} | |
} | |
private static void DivisoresNaturales() | |
{ | |
Console.WriteLine("Ingrese un numero para calcular sus divisores naturales"); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
for (var i = 1; i <= numero; i++) | |
{ | |
if (numero % i == 0) | |
{ | |
Console.WriteLine($"{i} es divisor natural de {numero}"); | |
} | |
} | |
} | |
private static void DivisiblesAlMismoTiempo() | |
{ | |
Console.WriteLine("Listar divisibles por 2, 3 y 5 al mismo tiempo\n Ingrese un numero. Ingrese -1 para terminar"); | |
var numeros = new List<int>(); | |
var numero = Convert.ToInt32(Console.ReadLine()); | |
while (numero != -1) | |
{ | |
if ((numero % 2 == 0 && numero % 3 == 0 && numero % 5 == 0)) | |
numeros.Add(numero); | |
Console.WriteLine("Ingrese otro numero o -1 para terminar"); | |
numero = Convert.ToInt32(Console.ReadLine()); | |
} | |
if (numeros.Count == 0) | |
Console.WriteLine("No ingreso numeros"); | |
else | |
numeros.ForEach(x => Console.WriteLine($"Numero {x} cumple con la condicion")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment