Java Ejercicios

32
>> ARITMETICA << - Hallar A+B-C+100 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) 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))); } }

description

ejercicion java

Transcript of Java Ejercicios

  • >> ARITMETICA
  • Leer un numeo de tres digitos y sumarlos

    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

  • 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

    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

    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

  • }

    }

    >> VECTORES

  • V [fin1] = aux1;

    fin1--;

    }

    fin1 = d;

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

  • }

    void mostrar (int V [], int d)

    {

    for (int i = 1 ; i

  • h.llenar (V, d);

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

    h.mostrar (V, d);

    h.evalua (V, d, x);

    }

    }

    ----------------------------------------------------------------------------------------------------------------------------

    Dado un vector ordenarlo de forma ascendente y desendente

    class ej1

    {

    void llenar (int V [], int d)

    {

    for (int i = 1 ; i

  • aux = V [i];

    V [i] = V [j];

    V [j] = aux;

    }

    }

    }

    }

    void orden_asc (int V [], int d)

    {

    for (int i = 1 ; i

  • }

    }

    >> MATRICES

  • for (int j = 1 ; j
  • for (int i = 1 ; i
  • }

    }

    -----------------------------------------------------------------------------------------------------------------------------------

    /*Dada una matris cuadrada invertir su diagonal principal*/

    class JavaMatrices2

    {

    void llenar (int M [] [], int d)

    {

    for (int i = 1 ; i

  • int aux = M [i] [i];

    M [i] [i] = M [d] [d];

    M [d] [d] = aux;

    fin--;

    }

    }

    public static void main (String args [])

    {

    JavaMatrices2 h = new JavaMatrices2 ();

    int M [] [] = new int [20] [20];

    System.out.print ("Inserte dimen. de la matris cuadrada: ");

    int d = Leer.datoInt ();

    h.llenar (M, d);

    System.out.print ("\nMATRIS ORIGINAL: ");

    h.mostrar (M, d);

    System.out.print ("\n\nMATRIS CON LA DIAGONAL PRINCIPAL INVERTIDA: ");

    h.invierte (M, d);

    h.mostrar (M, d);

    }

    }

    --------------------------------------------------------------------------------------------------------------------------------

    /*Dada una matris cuadrada invertir su diagonal secundaria*/

    class JavaMatrices3

    {

    void llenar (int M [] [], int d)

    {

    for (int i = 1 ; i

  • void mostrar (int M [] [], int d)

    {

    for (int i = 1 ; i

  • ------------------------------------------------------------------------------------------------------------------------------

    /*Dada dos matrices de diferentes tamanios R y S mostrar los elementos comunes de R en S*/

    class JavaMatrices4

    {

    void llenar (int M [] [], int f, int c)

    {

    for (int i = 1 ; i

  • {

    for (int k = 1 ; k

  • -----------------------------------------------------------------------------------------------------------------------------------

    /*Dada una matris intercambiar los elementos de la primera columna con la ultima columna*/

    class JavaMatrices5

    {

    void llenar (int M [] [], int f, int c)

    {

    for (int i = 1 ; i

  • }

    }

    public static void main (String args [])

    {

    JavaMatrices5 h = new JavaMatrices5 ();

    int M [] [] = new int [20] [20];

    System.out.print ("Inserte filas de la matris: ");

    int f = Leer.datoInt ();

    System.out.print ("Inserte columnas de la matris: ");

    int c = Leer.datoInt ();

    System.out.print ("\nLLENANDO MATRIS : \n");

    h.llenar (M, f, c);

    System.out.print ("\nLA MATRIS ORIGINAL : ");

    h.mostrar (M, f, c);

    System.out.print ("\n\nLA MATRICES INTERCAMBIADA : ");

    h.intercambiar (M, f, c);

    h.mostrar (M, f, c);

    }

    }

    --------------------------------------------------------------------------------------------------------------------

    /* Contar el numero de digitos de cada elemento de una matris */

    class JavaMatrices6

    {

    void llenar (int M [] [], int f, int c)

    {

    for (int i = 1 ; i

  • {

    for (int i = 1 ; i

  • int f = Leer.datoInt ();

    System.out.print ("Inserte columnas de la matris: ");

    int c = Leer.datoInt ();

    System.out.print ("\nLLENANDO MATRIS M: \n");

    h.llenar (M, f, c);

    System.out.print ("\nLA MATRIS: ");

    h.mostrar (M, f, c);

    System.out.print ("\n\nCONTEO DE DIGITOS: ");

    h.cuenta (M, f, c);

    }

    }

    ---------------------------------------------------------------------------------------------------------------------------

    >> MATRICES Y VECTORES

  • for (int j = 1 ; j
  • System.out.println ("\n\nLa columna " + i + " es igual al vector");

    }

    }

    public static void main (String args [])

    {

    JavaMatrisVector2 h = new JavaMatrisVector2 ();

    int M [] [] = new int [20] [20];

    int V [] = new int [20];

    System.out.print ("Inserte filas de la matris: ");

    int f = Leer.datoInt ();

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

    int d = Leer.datoInt ();

    System.out.print ("\nLLENANDO MATRIS: \n");

    h.llenarMatris (M, f, d);

    System.out.print ("\nLLENANDO EL VECTOR: \n");

    h.llenarVector (V, d);

    System.out.print ("\nLA MATRIS: ");

    h.mostrarMatris (M, f, d);

    System.out.print ("\n\nEL VECTOR: \n");

    h.mostrarVector (V, d);

    h.procedure (M, f, d, V, d);

    }

    }

    ---------------------------------------------------------------------------------------------------------------------------------

    /*Dada una matris Z almacenar en un vector A la suma por sus columnas

    y en un vector B la suma por sus filas*/

    class JavaMatrisVector3

    {

    void llenarMatris (int M [] [], int f, int c)

    {

    for (int i = 1 ; i

  • {

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

    M [i] [j] = Leer.datoInt ();

    }

    }

    }

    void mostrarMatris (int M [] [], int f, int c)

    {

    for (int i = 1 ; i

  • void vectorB (int M [] [], int f, int c, int B [], int d)

    {

    for (int i = 1 ; i

  • -----------------------------------------------------------------------------------------------------------------------

    /*Sumar dos vectores recursivamente*/

    class restaVectores

    {

    void llenar (int V [], int d)

    {

    for (int i = 1 ; i

  • int A [] = new int [20];

    int B [] = new int [20];

    int C [] = new int [20];

    System.out.println ("VECTOR A");

    h.llenar (A, d);

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

    h.llenar (B, d);

    h.suma (A, B, C, d);

    System.out.println ("\n\nVECTOR A");

    h.mostrar (A, d);

    System.out.println ("\n\nVECTOR B");

    h.mostrar (B, d);

    System.out.println ("\n\nVECTOR RESULTANTE C");

    h.mostrar (C, d);

    }

    }

    -----------------------------------------------------------------------------------------------------------------------------------

    /*restar dos vectores recursivamente*/

    class restaVectores

    {

    void llenar (int V [], int d)

    {

    for (int i = 1 ; i

  • }

    void resta (int A [], int B [], int C [], int i)

    {

    if (i != 0)

    {

    C [i] = A [i] - B [i];

    resta (A, B, C, i - 1);

    }

    }

    public static void main (String args [])

    {

    restaVectores h = new restaVectores ();

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

    int d = Leer.datoInt ();

    int A [] = new int [20];

    int B [] = new int [20];

    int C [] = new int [20];

    System.out.println ("VECTOR A");

    h.llenar (A, d);

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

    h.llenar (B, d);

    h.resta (A, B, C, d);

    System.out.println ("\n\nVECTOR A");

    h.mostrar (A, d);

    System.out.println ("\n\nVECTOR B");

    h.mostrar (B, d);

    System.out.println ("\n\nVECTOR RESULTANTE C");

    h.mostrar (C, d);

    }

    }

    --------------------------------------------------------------------------------------------------------------------------------

    /*multiplicar dos vectores recursivamente*/

  • class multiplicarVectores

    {

    void llenar (int V [], int d)

    {

    for (int i = 1 ; i

  • h.llenar (A, d);

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

    h.llenar (B, d);

    h.multipli (A, B, C, d);

    System.out.println ("\n\nVECTOR A");

    h.mostrar (A, d);

    System.out.println ("\n\nVECTOR B");

    h.mostrar (B, d);

    System.out.println ("\n\nVECTOR RESULTANTE C");

    h.mostrar (C, d);

    }

    }

    ---------------------------------------------------------------------------------------------------------------------------------

    class JavaAritmetica1

    import java.io.*;

    {

    public static int datoInt(){

    try{

    BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

    return Integer.parseInt(b.readLine());

    }catch(Exception e){

    e.printStackTrace();

    return -1;

    }

    }

    public static void main (String mago [])

    {

    int A, B, C;

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

    A = JavaAritmetica1.datoInt ();

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

  • B = JavaAritmetica1.datoInt ();

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

    C = JavaAritmetica1.datoInt ();

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

    100));

    }

    }