ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Ejercicios Resueltos De Java

agabrielpuerto19 de Agosto de 2013

4.436 Palabras (18 Páginas)779 Visitas

Página 1 de 18

EJERCICIOS RESUELTOS DE JAVA

________________________________________

>> ARITMETICA <<

- Hallar A+B-C+100

Código:

class JavaAritmetica1

{

public static void main (String mago [])

{

int A, B, C;

System.out.print ("Inserte A: ");

A = Leer.datoInt ();

System.out.print ("Inserte B: ");

B = Leer.datoInt ();

System.out.print ("Inserte C: ");

C = Leer.datoInt ();

System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));

}

}

Hallar (a-b)(a+b)

Código:

class JavaAritmetica2

{

public static void main (String elMago [])

{

int a, b;

System.out.print ("Inserte valor a: ");

a = Leer.datoInt ();

System.out.print ("Inserte valor b: ");

b = Leer.datoInt ();

System.out.println ("(" + a + "-" + b + ") " + "(" + a + "+" + b + ") " + "= " + ((a - b) * (a + b)));

}

}

Leer un numeo de tres digitos y sumarlos

Código:

class JavaAritmetica3

{

public static void main (String elMago [])

{

int numero, sumDig = 0;

System.out.print ("Inserte numero de tres digitos: ");

numero = Leer.datoInt ();

if (numero <= 100)

System.out.println ("ERROR: El numero no tiene 3 digitos");

else

{

int aux = numero; //en aux salvamos numero

while (numero != 0)

{

sumDig = sumDig + (numero % 10); //sumamos a sumDig el ultimo digito de numero

numero = numero / 10; //eliminamos el ultimo digito de numero

}

System.out.println ("La suma de los digitos de " + aux + " es: " + sumDig);

}

}

}

Dado un numero verificar:

- Que tenga dos digitos

- Verificar si sus digitos son pares

- Promediar sus digitos

Código:

class JavaAritmetica4

{

public static void main (String args [])

{

int numero;

System.out.print ("Inserte un numero de dos digitos pares: ");

numero = Leer.datoInt ();

int aux = numero;

if (numero < 100 && numero > 9)

{

int d1 = numero % 10;

numero = numero / 10;

int d2 = numero % 10;

if (d1 % 2 == 0 && d2 % 2 == 0)

System.out.println ("El promedio de los digitos de: " + aux + " es: " + ((d1 + d2) / 2));

}

}

}

Dado un numero entero, determinar si es positivo, negativo o nulo

Código:

class JavaAritmetica5

{

public static void main (String args [])

{

int numero;

System.out.print ("Inserte un numero: ");

numero = Leer.datoInt ();

if (numero == 0)

System.out.println ("El numero " + numero + " es NULO");

else

{

if (numero < 0)

System.out.println ("El numero " + numero + " es NEGATIVO");

else

System.out.println ("El numero " + numero + " es POSITIVO");

}

}

}

Dados seis numero determinar el menor de ellos

Código:

class JavaAritmetica6

{

public static void main (String args [])

{

int a, b, c, d, e, f;

System.out.print ("Inserte num.1: ");

a = Leer.datoInt ();

System.out.print ("Inserte num.2: ");

b = Leer.datoInt ();

System.out.print ("Inserte num.3: ");

c = Leer.datoInt ();

System.out.print ("Inserte num.4: ");

d = Leer.datoInt ();

System.out.print ("Inserte num.5: ");

e = Leer.datoInt ();

System.out.print ("Inserte num.6: ");

f = Leer.datoInt ();

int menor = a;

if (b < menor)

menor = b;

if (c < menor)

menor = c;

if (d < menor)

menor = d;

if (e < menor)

menor = e;

if (f < menor)

menor = f;

System.out.println ("\nEl menor de:" + a + "," + b + "," + c + "," + d + "," + e + "," + f + ",");

System.out.println ("Es: " + menor);

}

}

________________________________________

>> SERIES <<

Generar 5,10,15,20,25,30,...

Código:

class JavaSeries1

{

public static void main (String args [])

{

int n, c = 1, serie = 5;

System.out.print ("Cantidad d terminos: ");

n = Leer.datoInt ();

while (c <= n)

{

System.out.print ("," + serie);

serie += 5;

c++;

}

}

}

Si n=7 generar 7,6,5,4,3,2,1

Código:

class JavaSeries2

{

public static void main (String args [])

{

int n, c = 1;

System.out.print ("Cantidad d terminos: ");

n = Leer.datoInt ();

int serie = n;

while (c <= n)

{

System.out.print (serie + ",");

serie--;

c++;

}

}

}

________________________________________

>> VECTORES <<

/*Dado el vector T de tamao n. Si el tamao es par invertir los elementos de la mitad de los elementos

Ejemplo: v=[1][2][3][4][5][6] v(invertido)=[3][2][1][6][5][4]

*/

Código:

class JavaVectores1

{

void llenar (int V [], int d)

{

for (int i = 1 ; i <= d ; i++)

{

System.out.print ("Inserte pos.[" + i + "]: ");

V [i] = Leer.datoInt ();

}

}

void mostrar (int V [], int d)

{

for (int i = 1 ; i <= d ; i++)

{

System.out.print ("[" + V [i] + "]");

}

}

void invierte (int V [], int d)

{

int aux1;

int fin1 = d / 2;

for (int i = 1 ; i <= (d / 2) / 2 ; i++)

{

aux1 = V [i];

V [i] = V [fin1];

V [fin1] = aux1;

fin1--;

}

fin1 = d;

for (int j = (d / 2) + 1 ; j <= (d / 2) + 1 ; j++)

{

aux1 = V [j];

V [j] = V [fin1];

V [fin1] = aux1;

fin1--;

}

}

public static void main (String args [])

{

JavaVectores1 h = new JavaVectores1 ();

int V [] = new int [20];

System.out.print ("Inserte dimen. del vector: ");

int d = Leer.datoInt ();

h.llenar (V, d);

System.out.println ("\nVECTOR ORIGINAL: ");

h.mostrar (V, d);

System.out.println ("\nVECTOR LUEGO DE LA INVERSION: ");

h.invierte (V, d);

h.mostrar (V, d);

}

}

/*Dado un polinomio evualuarlo en el punto x (todo en un vector)*/

Código:

class JavaVectores2

{

void llenar (int V [], int d)

{

for (int i = 1 ; i <= d ; i++)

{

System.out.print ("Inserte pos.[" + i + "]: ");

V [i] = Leer.datoInt ();

}

}

void mostrar (int V [], int d)

{

for (int i = 1 ; i <= d ; i++)

{

System.out.print ("[" + V [i] + "]");

}

}

int potencia (int b, int e)

{

int p = 1;

for (int i = 1 ; i <= e ; i++)

{

p = p * b;

}

return (p);

}

void evalua (int V [], int d, int x)

{

int s = 0;

for (int i = 1 ; i <= d ; i += 2)

{

s = s + (V [i] * potencia (x, V [i + 1]));

}

System.out.println ("\n\nX es igual a: " + s);

}

public static void main (String args [])

{

JavaVectores2 h = new JavaVectores2 ();

int V [] = new int [20];

System.out.print ("Inserte dimen. del vector: ");

int d = Leer.datoInt ();

System.out.print ("Inserte valor de (x): ");

int x = Leer.datoInt ();

h.llenar (V, d);

System.out.println ("\nVECTOR: ");

h.mostrar (V, d);

h.evalua (V, d, x);

}

}

________________________________________

...

Descargar como (para miembros actualizados) txt (21 Kb)
Leer 17 páginas más »
Disponible sólo en Clubensayos.com