You are on page 1of 26

Facultad de Informática y Ciencias Aplicadas

PRIMERA TAREA PROGRAMACION I


PROGRAMAS EN C#

Tabla de Contenido

1.Introduccion.........................................................................................2

2.Desarrollo de Tarea..............................................................................3

2.1.Ejercicio # 1......................................................................................3

2.2.Ejercicio # 2......................................................................................7

2.3.Ejercicio # 3......................................................................................9

2.4.Ejercicio # 4.....................................................................................10

2.5.Ejercicio # 5.....................................................................................12

2.6.Ejercicio # 6.....................................................................................13

2.7.Ejercicio # 7.....................................................................................18

2.8.Ejercicio # 8.....................................................................................20

2.9.Ejercicio # 9.....................................................................................21

2.10.Ejercicio # 10.................................................................................22

2.11.Ejercicio # 11.................................................................................25
1. Introduccion

Por medio del presente trabajo hemos iniciado nuestro aprendizaje del lenguaje
de programación C sharp (C#), desarrollando los ejercicios contenidos en la
guía proporcionada por el instructor.

Dada nuestra “novatez” en el lenguaje, posiblemente nuestros programas aun


estén carentes de la optimización o los algoritmos mas eficientes, sin embargo
hemos hecho nuestro mejor esfuerzo en la resolución de los problemas
encomendados.

-2-
2. Desarrollo de Tarea
2.1. Ejercicio # 1

Realizar un programa que lea 50 números y muestre aquel o aquellos que


hayan aparecido más de 10 ocasiones.

using System;
{
static void Main(string[] args)
{
int[] cincnums = new int[50];
int[] repetmasdiezveces = new int[6];
int
i,j,k,contnums,numeroactual,banderarepetido,repeticiones,numerosrepetidos;
for (i = 0; i < 50; i++)
{
contnums = i + 1;
Console.WriteLine("Ingrese numero # "+contnums+ " :");
cincnums[i] = Convert.ToInt32(Console.ReadLine());
}
//una vez almacenados, se buscaran los repetidos mas de 10 veces
numerosrepetidos=0;
for (i = 0; i < 50; i++)
{
numeroactual = cincnums[i];
banderarepetido=0;
if (numerosrepetidos>0)
{
for (k=1;k<=numerosrepetidos;k++)
{
if (numeroactual==repetmasdiezveces[k])
{
banderarepetido=1;
}
}
}
if (banderarepetido==0) // El numero actual no fue detectado como
repetido
{
repeticiones = 0;
for (j = 0; j < 50; j
++)
{
if (numeroactual==cincnums[j])
{
repeticiones++;
}
}
if (repeticiones>=10)

-3-
{
Console.WriteLine("Numero Repetido mas de 10 Veces !!!:
"+numeroactual);
numerosrepetidos++;
repetmasdiezveces[numerosrepetidos]=numeroactual;
}
}
else if (banderarepetido==1)
{
//Nada que hacer
}
} // Finaliza la verificacion
if (numerosrepetidos == 0) Console.WriteLine("No hubieron numeros
repetidos!!!");
Console.ReadKey();
}
}

Log de Corrida
Microsoft Windows XP [Versión 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Joel>cd \

C:\>cd Progra1

C:\Progra1>dir
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 7CFD-F5F9

Directorio de C:\Progra1

14/02/2010 01:03 <DIR> .


14/02/2010 01:03 <DIR> ..
14/02/2010 01:03 1.958 cincuentanums.cs
1 archivos 1.958 bytes
2 dirs 8.807.219.200 bytes libres

C:\Progra1>csc cincuentanums.cs
Compilador de Microsoft (R) Visual C# 2005 versión 8.00.50727.3053
para Microsoft (R) Windows (R) 2005 Framework versión 2.0.50727
(C) Microsoft Corporation 2001-2005. Reservados todos los derechos.

C:\Progra1>dir
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 7CFD-F5F9

Directorio de C:\Progra1

14/02/2010 01:03 <DIR> .


14/02/2010 01:03 <DIR> ..
14/02/2010 01:03 1.958 cincuentanums.cs
14/02/2010 01:03 3.584 cincuentanums.exe
2 archivos 5.542 bytes

-4-
2 dirs 8.807.211.008 bytes libres

C:\Progra1>cincuentanums.exe
Ingrese numero # 1 :
1
Ingrese numero # 2 :
3
Ingrese numero # 3 :
4
Ingrese numero # 4 :
5
Ingrese numero # 5 :
6
Ingrese numero # 6 :
7
Ingrese numero # 7 :
8
Ingrese numero # 8 :
9
Ingrese numero # 9 :
10
Ingrese numero # 10 :
1
Ingrese numero # 11 :
2
Ingrese numero # 12 :
4
Ingrese numero # 13 :
5
Ingrese numero # 14 :
11
Ingrese numero # 15 :
2
Ingrese numero # 16 :
11
Ingrese numero # 17 :
3
Ingrese numero # 18 :
11
Ingrese numero # 19 :
5
Ingrese numero # 20 :
11
Ingrese numero # 21 :
7
Ingrese numero # 22 :
11
Ingrese numero # 23 :
9
Ingrese numero # 24 :
11
Ingrese numero # 25 :
5
Ingrese numero # 26 :
11
Ingrese numero # 27 :
90
Ingrese numero # 28 :
11
Ingrese numero # 29 :
5
Ingrese numero # 30 :

-5-
5
Ingrese numero # 31 :
5
Ingrese numero # 32 :
5
Ingrese numero # 33 :
5
Ingrese numero # 34 :
5
Ingrese numero # 35 :
5
Ingrese numero # 36 :
5
Ingrese numero # 37 :
5
Ingrese numero # 38 :
5
Ingrese numero # 39 :
5
Ingrese numero # 40 :
11
Ingrese numero # 41 :
11
Ingrese numero # 42 :
11
Ingrese numero # 43 :
11
Ingrese numero # 44 :
11
Ingrese numero # 45 :
11
Ingrese numero # 46 :
11
Ingrese numero # 47 :
11
Ingrese numero # 48 :
77
Ingrese numero # 49 :
101
Ingrese numero # 50 :
1901
Numero Repetido mas de 10 Veces !!!: 5
Numero Repetido mas de 10 Veces !!!: 11

C:\Progra1>

-6-
2.2. Ejercicio # 2
Calcular el factorial de un número ingresado por un usuario.

using System;
class FactNum
{
static void Main(string[] args)
{
int numero = 0;
try
{
Console.Write("Ingrese un numero: ");
numero = int.Parse(Console.ReadLine());
}
catch (System.FormatException e)
{

Console.WriteLine(e.Message.ToString());
}
FactNum.Factorial(numero);
Console.Write(" El factorial de " + numero + " es: " +
FactNum.Factorial(numero));
Console.Read();

-7-
return;
}

static int Factorial(int numero)


{
int contador = 1, i = 0;
if (numero > 0)
{
for (i = 2; i <= numero; i++)
{
contador = Math.Abs(contador * i);
}
}
else
{
Console.Write("El número debe ser mayor que 0!");
}
return contador;

}
}

-8-
2.3. Ejercicio # 3
Dado un numero determinar cuántos dígitos tiene (no se deberá usar la propiedad
Length).

using System;
class digitos
{
static void Main(string[] args)
{
{
int num = 0;
int digs = 1;
Console.WriteLine("Digite un numero entero:");
num = Convert.ToInt32(Console.ReadLine());
int numero = num;
do
{
numero = numero / 10;
if (numero == 0)

-9-
{
}
else
{
digs = digs + 1;
}
} while (numero != 0);
Console.WriteLine("La cantidad de digitos es: " + digs);
Console.ReadLine();
}
}
}

2.4. Ejercicio # 4

Realizar una aplicación que lea las notas correspondientes (5 notas) a los alumnos
de un determinado curso, las almacene en una matriz y de cómo resultado las nota
media del curso, la nota mínima del curso, la nota mayor del curso, porcentaje de
aprobados y porcentajes de reprobados.
Nota: la cantidad de alumnos deberán ser digitadas por el usuario así determinar el
tamaño de la matriz

using System;
class notas
{
static void Main(string[] args)
{
double
notaingresada=0,acumuladornotas=0,notafinal=0,promediocurso=0,notamenor=0,
notamayor=0,porca=0,porcr=0;
int i,j,cantalum = 0,aprobados=0,reprobados=0;
Console.Write("Ingrese cantidad de alumnos a evaluar: ");
cantalum = Convert.ToInt32(Console.ReadLine());
double[,] notas = new double[cantalum,5];

- 10 -
double[] notascurso = new double[cantalum];
for (i = 0; i <cantalum; i++)
{
Console.WriteLine("ALUMNO # "+(i+1)+": ");
acumuladornotas = 0;
for (j=0;j<=4;j++)
{
do {
Console.Write("Ingrese nota Evaluacion # "+(j+1)+": ");
notaingresada = double.Parse(Console.ReadLine());
} while (!(notaingresada>0 && notaingresada <=10));
notas[i, j] = notaingresada;
acumuladornotas = notaingresada + acumuladornotas;
}
notafinal = acumuladornotas / 5;
notascurso[i] = notafinal;
Console.WriteLine("-- Nota final Alumno: "+notafinal+" --");
}
acumuladornotas = 0;
notamenor = 10;
notamayor = 0;
aprobados=0;
reprobados=0;
for (i = 0; i <cantalum; i++)
{
if (notascurso[i] < notamenor)
{
notamenor = notascurso[i];
}
if (notascurso[i] > notamayor)
{
notamayor = notascurso[i];
}
acumuladornotas = notascurso[i] + acumuladornotas;
if (notascurso[i] >=6.0)
{
aprobados++;
}
else
{
reprobados++;
}
}
promediocurso = acumuladornotas/cantalum;
porca=100*aprobados/cantalum;
porcr=100*reprobados/cantalum;
Console.WriteLine("***** REPORTE FINAL *****");
Console.WriteLine("La nota promedio del curso es: "+promediocurso);
Console.WriteLine("La nota menor es : "+notamenor);
Console.WriteLine("La nota mayor es : "+notamayor);
Console.WriteLine("% de Aprobados es : "+porca);

- 11 -
Console.WriteLine("% de Reprobados es : "+porcr);
Console.ReadKey();
}
}

2.5. Ejercicio # 5

Ingresar la estatura de 82 alumnos de programación I en una matriz unidimensional


ordenarlas de la estatura mayor a la estatura menor y mostrarla en pantalla; dentro
de las estaturas deberán ser mayor 1.50mts y menor 2.0 metros si en un dado caso
hubiera una estatura mayor o menor a la estimada se deberá utilizar el rango limite
ya estimado.
Nota: No se podrán usar arreglos adicionales para realizar el ordenamiento.

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

namespace ConsoleApplication5
{
class Ordenar
{
static void Main(string[] args)
{
float[] A = new float[10];
float aux=0.0f;
for(int i=0; i<A.Length;i++)

- 12 -
{
aux=float.Parse(Console.ReadLine());
if (aux)= (1.5 f & aux <=2.0 f) {A[i]=aux;}

else if (aux<1.5f);

A[i]=1.5f;

else if (aux> 2.0 f);


{
A[i]=2.0f;
}
}
for(int i=0; i<10; i++)
{
for(int j=0; j<9; j++)
{
if(A[j+1]>A[j])
{
aux=A[j];
A[j]=A[j+1];
A[j+1]=aux;
}
}
}
for(int x=0; x<10; x++)
{
Console.WriteLine(A[x]);
Console.ReadLine();
}
}
}
}

2.6. Ejercicio # 6
Crear un programa que muestre en letras la cantidad de un numero digitado de 0
-1000. Se le recomienda que almacene la información en un arreglo.

using System;
class unoamil
{
static void Main(string[] args)
{
string[] numeroaescribir = new string[3];
int numeroleidonum,i;
string numeroleidostr;
do {

- 13 -
Console.WriteLine("Ingrese numero entero entre 0 y 1,000: ");
numeroleidostr = (Console.ReadLine());
numeroleidonum = Convert.ToInt32(numeroleidostr);
} while (!(numeroleidonum>=0 && numeroleidonum<=1000));

if (numeroleidonum == 1000)
{
Console.WriteLine("El numero ingresado fue: " + numeroleidonum + " Mil");
}
else if (numeroleidonum == 0)
{
Console.WriteLine("El numero ingresado fue: " + numeroleidonum + "
Cero");
}
else
{
if (numeroleidostr.Length==3)
{
numeroaescribir[0] = numeroleidostr.Substring(0,1);
numeroaescribir[1] = numeroleidostr.Substring(1,1);
numeroaescribir[2] = numeroleidostr.Substring(2,1);
}
else if (numeroleidostr.Length==2)
{
numeroaescribir[0] = "0";
numeroaescribir[1] = numeroleidostr.Substring(0,1);
numeroaescribir[2] = numeroleidostr.Substring(1,1);
}
else if (numeroleidostr.Length==1)
{
numeroaescribir[0] = "0";
numeroaescribir[1] = "0";
numeroaescribir[2] = numeroleidostr.Substring(0,1);
}
else
{
}
if (numeroaescribir[0] == "0")
{
Console.Write("El numero ingresado fue: " + numeroleidonum);
}
else if (numeroaescribir[0] == "1")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + " Cien");
}
else if (numeroaescribir[0] == "2")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Doscientos");
}
else if (numeroaescribir[0] == "3")

- 14 -
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Trescientos");
}
else if (numeroaescribir[0] == "4")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Cuatrocientos");
}
else if (numeroaescribir[0] == "5")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Quinientos");
}
else if (numeroaescribir[0] == "6")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Seiscientos");
}
else if (numeroaescribir[0] == "7")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Setecientos");
}
else if (numeroaescribir[0] == "8")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Ochocientos");
}
else if (numeroaescribir[0] == "9")
{
Console.Write("El numero ingresado fue: " + numeroleidonum + "
Novecientos");
}
else
{
}
if (numeroaescribir[1] == "0")
{
}
else if (numeroaescribir[1] == "1")
{
Console.Write(" Diez");
}
else if (numeroaescribir[1] == "2")
{
Console.Write(" Veinte");
}
else if (numeroaescribir[1] == "3")
{
Console.Write(" Treinta");

- 15 -
}
else if (numeroaescribir[1] == "4")
{
Console.Write(" Cuarenta");
}
else if (numeroaescribir[1] == "5")
{
Console.Write(" Cincuenta");
}
else if (numeroaescribir[1] == "6")
{
Console.Write(" Sesenta");
}
else if (numeroaescribir[1] == "7")
{
Console.Write(" Setenta");
}
else if (numeroaescribir[1] == "8")
{
Console.Write(" Ochenta");
}
else if (numeroaescribir[1] == "9")
{
Console.Write(" Noventa");
}
else
{
}
if (numeroaescribir[2] == "0")
{
}
else if (numeroaescribir[2] == "1")
{
Console.WriteLine(" Uno");
}
else if (numeroaescribir[2] == "2")
{
Console.WriteLine(" Dos");
}
else if (numeroaescribir[2] == "3")
{
Console.WriteLine(" Tres");
}
else if (numeroaescribir[2] == "4")
{
Console.WriteLine(" Cuatro");
}
else if (numeroaescribir[2] == "5")
{
Console.WriteLine(" Cinco");
}

- 16 -
else if (numeroaescribir[2] == "6")
{
Console.WriteLine(" Seis");
}
else if (numeroaescribir[2] == "7")
{
Console.WriteLine(" Siete");
}
else if (numeroaescribir[2] == "8")
{
Console.WriteLine(" Ocho");
}
else if (numeroaescribir[2] == "9")
{
Console.WriteLine(" Nueve");
}
else
{
}
Console.ReadKey();
}

}
}

- 17 -
2.7. Ejercicio # 7
Hacer un programa que muestre el mensaje de acuerdo la edad ingresada: Si la
edad es de 0 a 10 años “niño”, si la edad es de 11 a 14 años “púber”, si la edad es
de 15 a 18 años “adolescente”, si la edad es de 19 a 25 años “joven”, si la edad es
de 26 a 65 años “adulto”, si la edad es mayor de 65 “anciano”.

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

namespace ConsoleApplication7
{
class Mensaje
{
static void Main(string[] args)
{
int n = 0;
Console.Write("Ingrese edad:");
n = Convert.ToInt32(Console.ReadLine());
if (n >= 0 & n <= 10)
Console.WriteLine("Niño");
else
if (n >= 11 & n <= 14)
Console.WriteLine("Puber");
else
if (n >= 15 & n <= 18)
Console.WriteLine("Adolescente");
else
if (n >= 19 & n <= 25)
Console.WriteLine("Joven");
else

- 18 -
if (n >= 26 & n <= 65)
Console.WriteLine("Adulto");
else
if (n > 65)
Console.WriteLine("Anciano");
Console.ReadLine();
}
}
}

- 19 -
2.8. Ejercicio # 8
Hacer un programa que registre 15 números en un arreglo de una dimensión y
muestre las posiciones que ocupan los números impares.

using System;
class quincenums
{
static void Main(string[] args)
{
int[] arreglo=new int[15];
int i;
Console.WriteLine("A continuacion ingrese 15 numeros enteros ...");
for (i = 0; i <15; i++)
{
Console.Write("# "+(i+1)+": ");
arreglo[i]=Convert.ToInt32(Console.ReadLine());
}
Console.Write("Posiciones del Arreglo con Numeros impares: ");
for (i = 0; i <15; i++)
{
if ((arreglo[i]%2==1))
{
Console.Write((i+1)+" ");
}
}
Console.WriteLine();
Console.ReadKey();
}
}

- 20 -
2.9. Ejercicio # 9

Hacer un programa que registre 20 números en un arreglo de una dimensión y muestre posteriormente
los elementos que contienen números múltiplos de 3.

using System;
class veintenums
{
static void Main(string[] args)
{
int[] arreglo=new int[20];
int i;
Console.WriteLine("A continuacion ingrese 20 numeros enteros ...");
for (i = 0; i <20; i++)
{
Console.Write("# "+(i+1)+": ");
arreglo[i]=Convert.ToInt32(Console.ReadLine());
}
Console.Write("Posiciones del Arreglo con Numeros multiplos de Tres (3): ");
for (i = 0; i <20; i++)
{
if ((arreglo[i]%3==0))
{
Console.Write((i+1)+" ");
}
}
Console.WriteLine();
Console.ReadKey();
}
}

- 21 -
2.10. Ejercicio # 10

Hacer un programa que muestre las siguientes figuras almacenándolas en un arreglo bidimensional y
luego mostrarlas en pantalla.

* ********** * **********
** ********* ** *********
*** ******** *** ********
**** ******* **** *******
***** ****** ***** ******
****** ***** ****** *****
******* **** ******* ****
******** *** ******** ***
********* ** ********* **
********** * ********** *

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

namespace ConsoleApplication10
{
class Patrones
{
static void Main(string[] args)
{
string[,] A = new string[5, 5];
int i, j, k; //i:contador de filas,j:contador de columnas,k:contador de
espacios
Console.WriteLine("°programa que muestra una serie de patrones°");
Console.WriteLine("Patron 1");
Console.WriteLine();
- 22 -
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
for (k = 1; k <= i; k++)
{
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine("Patron 2");
Console.WriteLine();
for (i = 1; i <= 10; i++)
{
for (j = i; j <= 10; j++)
{
Console.Write("*");
}
for (k = 1; k <= i; k++)
{
Console.Write(" ");
}
Console.WriteLine();
}
// Console.WriteLine();
Console.WriteLine("Patron 3");
// Console.WriteLine();
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 11 - i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Patron 4");
Console.WriteLine();
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10 - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)

- 23 -
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
Console.Read();
}
}
}

- 24 -
2.11. Ejercicio # 11
Hacer un programa que muestre los primeros 50 números de la serie “Fibonacci” de tal forma:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...

1+1 = 2
2+1 = 3
3+2 = 5
5+3 = 8
8+5 = 13……..

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

namespace ConsoleApplication11
{
class Fibonacci
{
static void Main(string[] args)
{
int numero_terminos = 50;
int anterior = 0;
int siguiente = 1;
int c = 1;
int termino = 0;
Console.WriteLine(anterior);
Console.WriteLine(siguiente);
while (c <= numero_terminos)
{
termino = anterior + siguiente;
anterior = siguiente;
siguiente = termino;
Console.WriteLine(termino);
c += 1;
}
Console.ReadLine();
}
}
- 25 -
}

- 26 -

You might also like