Você está na página 1de 7

El main es el mtodo principal

Application.Run(new Form1()); este es el punto de arranque de la aplicacin


El program.cs es el punto de arranque de la aplicacin
Class es la clase
Load significa cargar

Ejercicios prcticos
1. Crear un formulario que permita hallar la solucin de una ecuacin de
primer grado de la forma ax + b = 0, la solucin de este tipo de
ecuaciones est dado por x = -b/a con a diferente de cero.
private void button1_Click(object sender, EventArgs e)
{
double a, b, x;
a = Double.Parse(txta.Text);
b = Double.Parse(txtb.Text);
if (a != 0)
{
x = -b / a;
MessageBox.Show("La solucion es x =" +x);
}
else
MessageBox.Show("Error a debe ser diferente de cero");
}
private void btnlimpiar_Click(object sender, EventArgs e)
{
txta.Clear();
txtb.Clear();
}
private void btnsalir_Click(object sender, EventArgs e)
{
Close();
}
}

2. Ingresar 5 nmeros en 5 cuadros de texto de un formulario, luego


determinar cul es el mayor de los nmeros.
private void button1_Click(object sender, EventArgs e)
{
double a, b, c, d, n, m;
a = Double.Parse(txtnum1.Text);
b = Double.Parse(txtnum2.Text);
c = Double.Parse(txtnum3.Text);
d = Double.Parse(txtnum4.Text);
n = Double.Parse(txtnum5.Text);
if (a > b)
{
m = a;
}
else

{ m = b; }
if (c > m)
{
m = c;
}
if (d > m)
{
m = d;
}
if (n > m)
{
m = n;
}
MessageBox.Show("El mayor es:" + m);
}
private void btnlimpiar_Click(object sender, EventArgs e)
{
txtnum1.Clear();
txtnum2.Clear();
txtnum3.Clear();
txtnum4.Clear();
txtnum5.Clear();
}

private void button1_Click_1(object sender, EventArgs e)


{
Close();
}

3. Hallar la solucin de una ecuacin de segundo grado de la forma ax2 +


bx + c= 0.
private void btncalcular_Click(object sender, EventArgs e)
{
double a, b, c, x1, x2, x3, D;
a = Double.Parse(txta.Text);
if (a != 0)
{
b = Double.Parse(txtb.Text);
c = Double.Parse(txtc.Text);
D = b * b - 4 * a * c;
if (D > 0)
{
x1 = (-b + Math.Sqrt(D)) / (2 * a);
x2 = (-b - Math.Sqrt(D)) / (2 * a);
MessageBox.Show("Las soluciones son: x1 = " + x1 + " y " + "x2 = " + x2);
}
else
{
if (D == 0)
{
x3 = -b / (2 * a);
MessageBox.Show("La solucion es: x=" + x3);

}
else { MessageBox.Show("La ecuacin no tiene solucin en los reales"); }

}
}
else { MessageBox.Show("Error a debe ser diferente de cero"); }
}
private void btnlimpiar_Click(object sender, EventArgs e)
{
txta.Clear();
txtb.Clear();
txtc.Clear();
}

Etsunp.edu.pe/campus
Usuario:jmedinac9
Clave: $$%%&&mate
4. Crear un formulario que permita leer un nmero entero, luego
determinar la cantidad de divisores que tiene el nmero.
private void btncalcular_Click(object sender, EventArgs e)
{
int N, I, cantdiv;
N = Int32.Parse(txta.Text);
cantdiv = 1;
I = 2;
while (I <= N)
{
if (N % I == 0)
cantdiv = cantdiv + 1;
I = I + 1;
}
MessageBox.Show("La cantidad de divisores es:" + cantdiv);
}

5. Leer un nmero y me permita determinar si el nmero es primo o no.


int N, I;
bool esprimo;
N = Int32.Parse(txta.Text);
I = 2;
esprimo = true;
while (I <= N/2)
{
if (N % I == 0)
esprimo = false;
break;
}
I = I + 1;
if (esprimo == true && N>1)
MessageBox.Show("El numero es primo");
else
MessageBox.Show("El numero no es primo");

6. Crear un formulario que permita leer un numero entero y permita


determinar si el nmero es perfecto.
int N, I, sumdiv;
N = Int32.Parse(txta.Text);
sumdiv = 1;
I = 2;
while(I<=N/2)
{
if (N % I == 0)
sumdiv = sumdiv + I;
I = I + 1;
}
if (N == sumdiv)
MessageBox.Show("El numero es perfecto");
else
MessageBox.Show("No es perfecto");

Random nos permite generar nmeros aleatorios


7. Crear un formulario que permita llenar un listbox con 100 nmeros
comprendidos entre 0 y 50 y luego al hacer click en un botn calcular
mostrar cual es el mayor de los nmeros ingresados.
Creamos un onjeto aleatorio de la clase Ramdom
Ramdom Aleatorio=new Ramdow();
Por defecto oAleatorio.nextDouble(); da un numero aleatorio entre 0 y 1
Si queremos los 50 primeros multiplicamos por 50;
Numero=50* oAleatorio.nextDouble();
Sintaxis
private void FAleatorio_Load(object sender, EventArgs e)
{
Random oAleatorio = new Random();
int I, N=100;
double numero;
I = 1;
while (I <= N)
{
numero = 50 * oAleatorio.NextDouble();
listBox1.Items.Add(numero.ToString("#,##0.00"));
I++; //I=I+1;
}
}
private void btncalcular_Click(object sender, EventArgs e)
{
double mayor, numero;
mayor = Double.Parse(listBox1.Items[0].ToString());
for (int I = 1; I < 100; I++)
{
numero = Double.Parse(listBox1.Items[I].ToString());
if (numero > mayor)
mayor = numero;
}
MessageBox.Show("El numero mayor es:" + mayor);
}

}
}

8. Generar temperaturas entre 15 45


El mtodo next me devuelve un numero entero

String mensaje;
public String Ecuacion1(double CoefA, double CoefB)
{
double x;
if (CoefA != 0)
{
x = -CoefB / CoefA;
mensaje = "La solucion es x =" +x;
}
else
mensaje = "Error a debe ser diferente de cero";
return mensaje;
}
public String Ecuacion2(double CoefA, double CoefB, double CoefC)
{
double x1, x2, x3, D;
if (CoefA != 0)
{
D = CoefB * CoefB - 4 * CoefA * CoefC;
if (D > 0)
{
x1 = (-CoefB + Math.Sqrt(D)) / (2 * CoefA);
x2 = (-CoefB - Math.Sqrt(D)) / (2 * CoefA);
mensaje = "Las soluciones son: x1 = " + x1 + " y " + "x2 = " + x2;
}
else
{
if (D == 0)
{
x3 = -CoefB / (2 * CoefA);
mensaje = "La solucion es: x=" + x3;
}
else { mensaje = "La ecuacin no tiene solucin en los reales"; }
}
}
else { mensaje = "Error a debe ser diferente de cero"; }
return mensaje;
}

Primo
int N, I, cantdiv;
N = Int32.Parse(txta.Text);
cantdiv = 1;
I = 2;
while (I <= N)
{
if (N % I == 0)
cantdiv = cantdiv + 1;
I = I + 1;
}
if (cantdiv == 2)
MessageBox.Show("El numero es primo");
else
{
if (cantdiv == 1)
MessageBox.Show("El numero es simple");
else
MessageBox.Show("El numero no es primo");
}
int N, I, J = 0;
bool esprimo;
double[] Numero = new Double[100];
N = 2;
while(N<=100)
{ I=2;
esprimo = true;
while (I <= N/2)
{
if (N % I == 0)
{
esprimo = false;
break;
}
I = I + 1;
}
if(esprimo==true)
{
Numero[J] = N;
J = J + 1;
}
//listBox1.Items.Add(N.ToString());
N = N + 1;
}

double mayor, numero;


mayor = Double.Parse(listBox1.Items[0].ToString());
for (int I = 1; I < 100; I++)
{
numero = Double.Parse(listBox1.Items[I].ToString());
if (numero > mayor)
mayor = numero;
}
MessageBox.Show("El numero mayor es:" + mayor);

int tmax, tmin, temper, suma, ndias=365;


double tprom;
tmax = Int32.Parse(listBox1.Items[0].ToString());
tmin = Int32.Parse(listBox1.Items[0].ToString());
suma = tmax;
for(int I=1;I<365;I++)
{
// MessageBox.Show(I.ToString());
temper = Int32.Parse(listBox1.Items[I].ToString());
if (temper > tmax)
tmax = temper;
if (temper < tmin)
tmin = temper;
suma = suma + temper;
}
tprom = suma / ndias;
MessageBox.Show("La mayor de las temperaturas es:" + tmax + "\nLa menor es:" +
tmin + "\nLa temperatura promedio es:"+tprom);

Formulario
Matematica oMatematica = new Matematica();
MessageBox.Show(oMatematica.Ecuacion2(Double.Parse(txta.Text),
Double.Parse(txtb.Text), Double.Parse(txtc.Text)));

Você também pode gostar