1. Programación en C#
PÁG.- 1
DOCENTE : Ing. DAVID ALFONSO PONCE LÓPEZ
dponcelopez@hotmail.com
CURSO : LABORATORIO DE PROGRAMACIÓN III
ESTUDIANTES :
DÍAZ QUISPE, Darwin Alex
LABAJOS TUESTA, Diego José
SANDI RENGIFO, Linder
CONTAMANA – LORETO – PERÚ
2014
2. Programación en C#
PÁG.- 2
PROBLEMAS REPETITIVOS COMPUESTOS
EJERCICIO 1: Hallar la suma de un número entero más su inversa.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string numCad;
string invCad = "";
int num1;
int num2;
int R;
Console.Write("Ingrese un valor numérico: ");
numCad = Console.ReadLine();
num1 = int.Parse(numCad);
for (int i = numCad.Length - 1; i > -1; i--)
{
invCad += numCad[i];
}
num2 = int.Parse(invCad);
R = num1 + num2;
Console.WriteLine();
Console.WriteLine(num1 + " + ");
Console.WriteLine(num2);
Console.WriteLine("-------");
Console.WriteLine(R);
Console.ReadLine();
}
}
}
3. Programación en C#
PÁG.- 3
EJERCICIO 2: Realizar las Siguientes Serie:
11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 41 42 43 44 45 51 52 53 54 55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num=0;
int aux = 10;
Console.WriteLine("Serie de Números:");
Console.WriteLine("=================");
Console.WriteLine();
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
num = aux + j;
Console.Write(num + " ");
}
aux = num + 5;
}
Console.ReadLine();
}
}
}
EJERCICIO 3. Realizar un programa en C# que calcule en valor de “Y” para “n”
términos de la serie:
Y = X2/2! - (X4 / 4!) + (X6 / 6!) - (X8/8!) + .....
4. Programación en C#
PÁG.- 4
EJERCICIO 4: Calcule la suma de los términos de la serie FIBONACCI cuyos valores
se encuentran entre 100 y 10,000.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int A = 0;
int B = 1;
int C = A + B;
int suma = 0;
Console.WriteLine("Serie Fibonacci: ");
Console.Write(A + " ");
Console.Write(B + " ");
while (C < 10000)
{
Console.Write(C + " ");
if (C > 100 && C < 10000)
{
suma = suma + C;
}
A = B;
B = C;
C = A + B;
}
Console.WriteLine();
Console.WriteLine("La Suma es: " + suma);
Console.ReadLine();
}
}
}
5. Programación en C#
PÁG.- 5
EJERCICIO 5. Hallar los números primos menores que 100. Los números primos son
divisibles exactamente tan sólo por sí mismos y por la unidad.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int div = 0;
Console.WriteLine("NÚMEROS PRIMOS MENORES QUE 100");
Console.WriteLine("---------------------------------");
Console.WriteLine(" ");
for (int i = 1; i <=100; i++)
{
for (int j = 1; j <= i; j++)
{
if (i % j == 0)
{
div += 1;
}
}
if (div==1 || div == 2)
{
Console.Write(i + " ");
}
div = 0;
}
Console.ReadLine();
}
}
}
6. Programación en C#
PÁG.- 6
EJERCICIO 6. En una reunión asistieron “n” personas, realizar un programa para
calcular el número de saludos suponiendo que todos los presentes se saludaron.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
class calculo
{
public int factorial(int x)
{
int fact = 1;
for (int i = 1; i <= x; i++)
{
fact = fact * i;
}
return fact;
}
}
static void Main(string[] args)
{
int k;
int y;
int num;
int resul;
Console.Write("Ingrese cantidad de personas asistentes: ");
num = int.Parse(Console.ReadLine());
calculo objFact = new calculo();
k = 2;
y = num - k;
resul = objFact.factorial(num) / (objFact.factorial(k) * objFact.factorial(y));
Console.WriteLine();
Console.Write("Saludos efectuados: " + resul);
Console.ReadLine();
}
}
}
7. Programación en C#
PÁG.- 7
EJERCICIO 7. Suponga que tiene usted una tienda y desea registrar las ventas en una
computadora. Diseñe un y programa que lea por cada cliente, el monto total de su
compra. Al final del día escriba la cantidad total de las ventas y el número de clientes
atendidos.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string cliente;
int montoCliente;
int contCli=0;
int montoTotal=0;
string opc;
Console.WriteLine("REGISTRO DE VENTAS");
Console.WriteLine("==================");
Console.WriteLine();
Console.Write("Registrar venta (S/N)?: ");
opc = Console.ReadLine();
Console.WriteLine();
while (opc == "S" || opc == "s")
{
Console.Write("Nombre de Cliente: ");
cliente = Console.ReadLine();
contCli += 1;
Console.Write("Monto: ");
montoCliente = int.Parse(Console.ReadLine());
montoTotal += montoCliente;
Console.WriteLine();
Console.Write("Registrar venta (S/N)?: ");
opc = Console.ReadLine();
Console.WriteLine();
}
Console.WriteLine();
Console.Write("Cantidad total de las ventas: " + montoTotal);
Console.WriteLine();
Console.Write("Cantidad de Clientes atendidos: " + contCli);
Console.ReadLine();
}
}
}
8. Programación en C#
PÁG.- 8
EJERCICIO 8. Realizar un programa en C# que convierta un número del sistema de
base 2 al sistema decimal.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string numCad;
char num;
int aux;
long oper;
int iLength;
long iResult = 0;
int contNotBin = 0;
Console.WriteLine("CONVERTIR DE BINARIO A DECIMAL: ");
Console.WriteLine("================================");
Console.WriteLine();
Console.Write("Escribe un numero en binario: ");
numCad = Console.ReadLine();
iLength = numCad.Length;
iLength--;
for (int i = 0; i< numCad.Length; i++, iLength--)
{
num = numCad[i];
aux = Convert.ToInt32(num);
if (((aux - 48) == 1 )|| ((aux - 48) == 0)) {
oper = (long)Math.Pow(2, iLength);
iResult = iResult + (long)(aux - 48) * oper;
}
else {
contNotBin += 1;
}
}
Console.WriteLine();
if (contNotBin > 0) {
Console.Write(numCad + " NO es un número BINARIO");
}
else {
Console.Write("Valor decimal de " + numCad + " = " + iResult);
}
Console.ReadLine();
}
}
}
9. Programación en C#
PÁG.- 9
EJERCICIO 9. Realizar un programa en C# que convierta un número del sistema
decimal al sistema octal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;
int r;
int [ ] octal = new int[20];
int cont = 0;
Console.Write("Valor Decimal: ");
num = int.Parse(Console.ReadLine());
r = num / 8;
octal[cont] = num % 8;
num = r;
while (num>8)
{
cont += 1;
octal[cont] = (num % 8);
r = num / 8;
num = r;
}
cont += 1;
octal[cont] = r;
Console.WriteLine();
Console.Write("Valor Octal: ");
for (int k = cont; k>=0; k--)
{
Console.Write(octal[k]);
}
Console.ReadLine();
}
}
}
10. Programación en C#
PÁG.- 10
EJERCICIO 10. Calcule la suma de los términos primos de la serie FIBONACCI
menores que “n”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class SumaPrimos
{
static void Main(string[] args)
{
long[] Serie = new long[30];
long suma = 0;
int cind = 0;
int A = 0;
int B = 1;
int C = A + B;
int cont = 3;
int num;
int div=0;
Serie[0] = A;
Serie[1] = B;
Serie[2] = C;
Console.WriteLine("Serie Fibonacci: ");
Console.WriteLine("=================");
Console.Write(Serie[0] + " ");
Console.Write(Serie[1] + " ");
Console.Write(Serie[2] + " ");
while (C < 10000)
{
A = B;
B = C;
C = A + B;
Serie[cont] = C;
Console.Write(Serie[cont] + " ");
cont += 1;
}
Console.WriteLine();
11. Programación en C#
PÁG.- 11
Console.WriteLine();
Console.Write("Ingrese un valor numérico: ");
num = int.Parse(Console.ReadLine());
for (int k = 0; k < cont; k++)
{
if (Serie[k]<=num)
{
cind = k+1;
}
}
Console.WriteLine();
Console.WriteLine("Números primos de la serie Fibonacci menores que "
+ num + ":");
for (int i = 0; i < cind; i++)
{
for (int j = 1; j <= i; j++)
{
if (Serie[i] % j == 0)
{
div += 1;
}
}
if (div == 1 || div == 2)
{
Console.Write(Serie[i] + " ");
suma = suma + Serie[i];
}
div = 0;
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Suma: " + suma);
Console.ReadLine();
}
}
}
12. Programación en C#
PÁG.- 12
ARREGLOS Y ESTRUCTURAS
1. Sea la estructura:
Alumno
Nombre
Edad
Nota
Realizar un programa para ordenar “n” alumnos por nota.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ejercicio01_Arreglos
{
public struct Alumno
{
public string Nombre;
public int Edad;
public double Nota;
}
class Program
{
static void Main(string[] args)
{
int N;
Console.Write("Ingrese la Cantidad de Alumnos a Registrar: ");
N = int.Parse(Console.ReadLine());
Alumno[] A;
A = new Alumno[N];
for (int i = 0; i < N; i++)
{
Console.WriteLine();
Console.WriteLine("Ingrese Datos del Alumno " + (i + 1) + " :");
Console.WriteLine();
Console.Write("Nombre: ");
A[i].Nombre = Console.ReadLine();
Console.Write("Edad: ");
A[i].Edad = int.Parse(Console.ReadLine());
Console.Write("Nota: ");
A[i].Nota = double.Parse(Console.ReadLine());
Console.WriteLine();
}
13. Programación en C#
PÁG.- 13
Alumno Aux;
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (A[i].Nota > A[j].Nota)
{
Aux = A[i];
A[i] = A[j];
A[j] = Aux;
}
}
}
Console.WriteLine("Presione Enter ordenar lista de alumnos por Nota.");
Console.ReadLine();
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < N; i++)
{
Console.Write(A[i].Nombre + " " + A[i].Edad + " " + A[i].Nota);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
2. Sea la estructura:
Alumno
Nombre
Edad
Nota
Sexo
Realizar un programa para ordenar la lista según sexo y nota.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public struct Alumno
{
public string Nombre;
public int Edad;
14. Programación en C#
PÁG.- 14
public double Nota;
public char sexo;
}
class Program
{
static void Main(string[] args)
{
int N;
Console.Write("Cantidad de Alumnos a Registrar: ");
N = int.Parse(Console.ReadLine());
Alumno[] A;
A = new Alumno[N];
for (int i = 0; i < N; i++)
{
Console.WriteLine("Datos del Alumno " + (i + 1) + " :");
Console.WriteLine();
Console.Write("Nombre: ");
A[i].Nombre = Console.ReadLine();
Console.Write("Edad: ");
A[i].Edad = int.Parse(Console.ReadLine());
Console.Write("Nota: ");
A[i].Nota = double.Parse(Console.ReadLine());
Console.Write("Sexo <M/F>: ");
A[i].sexo = char.Parse(Console.ReadLine());
Console.WriteLine();
}
Alumno Aux;
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (A[i].Nota < A[j].Nota)
{
Aux = A[i];
A[i] = A[j];
A[j] = Aux;
}
}
}
Console.WriteLine("Presione Enter para Ordenar lista por Sexo y Nota.");
Console.ReadLine();
Console.WriteLine();
for (int i = 0; i < N; i++)
{
Console.Write(A[i].Nombre + " " + A[i].sexo + " " + A[i].Nota);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
15. Programación en C#
PÁG.- 15
3. Sea la estructura:
Docente
Nombre
Sexo
Experienciaaños
Realizar un programa para ordenar “n” docentes en forma descendente, según
experiencia.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ejercicio03_Vectores
{
public struct Docente
{
public string Nombre;
public char Sexo;
public int Year_Experiencia;
}
class Program
{
static void Main(string[] args)
{
int N;
Console.WriteLine();
Console.Write("Ingrese la Cantidad de Docentes a Registrar: ");
N = int.Parse(Console.ReadLine());
Docente[] D;
D = new Docente[N];
for (int i = 0; i < N; i++)
{
Console.WriteLine();
Console.WriteLine("Ingrese Datos del Docente " + (i + 1) + " :");
Console.WriteLine();
Console.Write("Nombre: ");
D[i].Nombre = Console.ReadLine();
Console.Write("Sexo <M/F>: ");
D[i].Sexo = char.Parse(Console.ReadLine());
Console.Write("Años de Experiencia: ");
D[i].Year_Experiencia = int.Parse(Console.ReadLine());
Console.WriteLine();
}
Docente Aux;
16. Programación en C#
PÁG.- 16
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (D[i].Year_Experiencia < D[j].Year_Experiencia)
{
Aux = D[i];
D[i] = D[j];
D[j] = Aux;
}
}
}
Console.WriteLine("Presione Enter para Mostrar la Lista de Docentes.");
Console.WriteLine("Ordenados por Año de Experiencia.");
Console.ReadLine();
Console.WriteLine();
for (int i = 0; i < N; i++)
{
Console.Write(D[i].Nombre + " " + D[i].Sexo + " " + D[i].Year_Experiencia);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
4. Sea la estructura:
Docente
Nombre
Profesión
Sexo
Experienciaaños
Realizar un programa para ordenar los docentes según orden alfabético.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ejercicio04_Arreglos
{
class Program
{
public struct Docente
{
public string Nombre;
public string Profesion;
public char Sexo;
public int Year_Experiencia;
}
17. Programación en C#
PÁG.- 17
static void Main(string[] args)
{
int N;
int Opc;
Console.Write("Cantidad de Registros: ");
N = int.Parse(Console.ReadLine());
Docente[] D;
D = new Docente[N];
for (int i = 0; i < N; i++)
{
Console.WriteLine("Datos del Docente" + (i + 1) + ":");
Console.WriteLine();
Console.Write("Nombre: ");
D[i].Nombre = Console.ReadLine();
Console.Write("Profesion: ");
D[i].Profesion = Console.ReadLine();
Console.Write("Sexo <M/F>: ");
D[i].Sexo = char.Parse(Console.ReadLine());
Console.Write("Años de Experiencia: ");
D[i].Year_Experiencia = int.Parse(Console.ReadLine());
Console.WriteLine();
}
Docente Aux;
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
Opc = String.Compare(D[i].Nombre, D[j].Nombre);
if (Opc == 1)
{
Aux = D[i];
D[i] = D[j];
D[j] = Aux;
}
}
}
Console.WriteLine("Presione Enter para ordenar alfabéticamente");
Console.ReadLine();
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < N; i++)
{
Console.Write(D[i].Nombre + " " + D[i].Profesion + " " + D[i].Sexo + " "
+ D[i].Year_Experiencia);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
18. Programación en C#
PÁG.- 18
5. Dada la siguiente lista de números:
31 9 29 63 52 39 100 83 23 4
Ordenarlos.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ejercicios_UPP
{
class program
{
static void Main(string[] args)
{
int[] Numeros;
Numeros=new int [] {31,9,29,63,52,39,100,83,23,4};
Console.WriteLine("Los Números a Ordenar Son:");
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.Write( Numeros[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Presione Enter Para ordenar los Numeros ....!!!");
Console.WriteLine();
Console.ReadLine();
int Aux;
for (int i = 0; i < 10; i++)
{
for (int j = i+1; j < 10; j++)
{
if (Numeros[i] > Numeros[j])
{
Aux=Numeros[i];
Numeros[i]=Numeros[j];
Numeros[j] = Aux;
}
}
}
Console.WriteLine("Los Números Ordenados Son:");
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.Write(Numeros[i] + " ");
}
Console.ReadLine();
}
}
}
19. Programación en C#
PÁG.- 19
6. Dada una lista de “n” nombres, realizar un programa que inserte cualquier nombre
pero tomando en cuenta que la lista esté ordenada.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public struct nombres
{
public string Nombre;
}
class Program
{
static void Main(string[] args)
{
int Opc;
int x;
int N;
string newNombre;
Console.Write("Cantidad de Registros: ");
N = int.Parse(Console.ReadLine());
Console.WriteLine();
nombres[] D;
D = new nombres[N];
x = N + 1;
nombres[] A;
A = new nombres[x];
for (int i = 0; i < N; i++)
{
Console.Write("Nombre " + (i + 1) + ": ");
D[i].Nombre = Console.ReadLine();
}
nombres Aux;
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
Opc = String.Compare(D[i].Nombre, D[j].Nombre);
if (Opc == 1)
{
Aux = D[i];
D[i] = D[j];
D[j] = Aux;
20. Programación en C#
PÁG.- 20
}
}
}
Console.WriteLine();
Console.WriteLine("Nombres ordenados: ");
Console.WriteLine("====================");
Console.WriteLine();
for (int i = 0; i < N; i++)
{
Console.WriteLine(D[i].Nombre);
}
Console.WriteLine();
Console.Write("Nuevo nombre a insertar: ");
newNombre = Console.ReadLine();
A[0].Nombre = newNombre;
for (int i = 0; i <N; i++)
{
A[i+1].Nombre = D[i].Nombre;
}
for (int i = 0; i < x; i++)
{
for (int j = i + 1; j <=N; j++)
{
Opc = String.Compare(A[i].Nombre, A[j].Nombre);
if (Opc == 1)
{
Aux = A[i];
A[i] = A[j];
A[j] = Aux;
}
}
}
Console.WriteLine();
Console.WriteLine("Nueva Lista ordenada de nombres: ");
Console.WriteLine("==================================");
Console.WriteLine();
for (int i = 0; i <x; i++)
{
Console.WriteLine(A[i].Nombre);
}
Console.ReadLine();
}
}
}