Sunteți pe pagina 1din 26

Licenciatura en Ingeniería en Computación.

Programación II.

Docente: Ing. José Luis García Morales.

TRABAJO:
Ejercicios curso Programación en Java de Taboada

Alumno: Jeremy Urbano Plata

Grupo: ICO-24.

Periodo Escolar: 2020A.


Ejercicios curso Programación en Java de Taboada

1. Leer 5 números, guardarlos en un arreglo y mostrarlos en el mismo orden introducido.

import java.util.Arrays;
import java.util.Scanner;

public class LeerNumeros {

public static void main(String[] args) {


System.out.println("Ingresa los numeros");
Scanner teclado = new Scanner(System.in);
float[] LeerNumeros = new float[5];
for (int i = 0; i < LeerNumeros.length; i++) {

System.out.print("ingresa el numero " + (i + 1) + " ");

LeerNumeros[i] = teclado.nextFloat();
}
for (float i : LeerNumeros) {
System.out.println("numero " + i);
}
System.out.println(Arrays.toString(LeerNumeros));

for (double i : LeerNumeros) {


System.out.println(i);
}

2. Leer 5 números, guardarlos en un arreglo y mostrarlos en el orden inverso introducido.


package ejercicio2;

import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio2 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
float[] numeros = new float[5];
System.out.println("ingresa los numeros");
for (int i = 0; i < 5; i++) {
System.out.print("ingresa el numero " + (i + 1) + " ");
numeros[i] = teclado.nextFloat();
}

System.out.println("\n LOS NUMERO EN INVERSO SON :");


for (int i = 4; i >= 0; i--) {
System.out.println(numeros[i]);

}
}
}

3. Leer 5 números, almacenarlos en un arreglo y a continuación realizar la media de los números


positivos, la media de los negativos y contar el número de ceros.
package ejercicio3;
import java.util.Scanner;
/**
*
* @author jeremy
*/
public class Ejercicio3 {

public static void main(String[] args) {


float NumPos = 0;
float NumNeg = 0;
int ConPos = 0;
int ConNeg = 0;
int ContCeros = 0;
float mediaPos = 0;
float mediaNeg = 0;
Scanner teclado = new Scanner(System.in);
System.out.println("Ingresa los numeros");
float[] LeerNumeros = new float[5];
for (int i = 0; i < 5; i++) {
System.out.print("ingresa el numero " + (i + 1) + " ");
LeerNumeros[i] = teclado.nextFloat();
if (LeerNumeros[i] == 0) {
ContCeros++;
} else if (LeerNumeros[i] > 0) {
NumPos += LeerNumeros[i];
ConPos++;
} else {
NumNeg += LeerNumeros[i];
ConNeg++;
}
}
System.out.println("EL NUMERO DE CEROS ES: ");
System.out.println(ContCeros);
if (ConPos == 0) {
System.out.println("no se puede sacra la media de los numeros positivos");
} else {
mediaPos = NumPos / ConPos;
System.out.println("el promedio de los positivos es : ");
System.out.println(mediaPos);
}

if (ConNeg == 0) {

} else {
mediaNeg = NumNeg / ConNeg;
}
System.out.println("el promedio de los negativos es : ");
System.out.println(mediaNeg);}}

4. Leer 10 números enteros, almacenarlos en un arreglo. Debemos mostrarlos en el siguiente orden: el


primero, el último, el segundo, el penúltimo, el tercero, etc.
package ejercicio4;

import java.util.Scanner;

public class Ejercicio4 {

public static void main(String[] args) {


int numeros[] = new int[10];
Scanner teclado = new Scanner(System.in);
System.out.println("digite sus 10 numeros");
for (int i = 0; i < 10; i++) {
System.out.print("ingrese el numero " + (i + 1) + " ");
numeros[i] = teclado.nextInt();

}
System.out.println("el resultado es");
for (int i = 0; i < 5; i++) {
System.out.print(numeros[i] + " ");
System.out.print(numeros[9 - i] + " ");
}

}
}
5. Leer por teclado dos tablas de 10 números enteros y mezclarlas en una tercera de la forma: el primero
de la A, el primero de la B, el segundo de la A, el segundo de la B, etc.
package ejercicio5;

import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio5 {
public static void main(String[] args) {
Scanner teclado = new Scanner (System.in);
int ArrayA[] = new int[10];
int ArrayB[] = new int[10];
int ArrayC[] = new int [20];
System.out.println(" INGRESA LOS VALORES DEL,AREGLO A");
for(int i=0;i<10;i++){
System.out.print("ingresa el valor "+(i+1)+" : ");
ArrayA[i]= teclado.nextInt();
}
System.out.println(" INGRESA LOS VALORES DEL,AREGLO B");
for(int i=0;i<10;i++){
System.out.print("ingresa el valor "+(i+1)+" : ");
ArrayB[i]= teclado.nextInt();
}
System.out.println("el resultado es el siguiente");
int j=0;
for(int i=0; i<10 ;i++){
ArrayC[j]= ArrayA[i];
j++;

ArrayC[j]= ArrayB[i];
j++;
}
System.out.println(Arrays.toString(ArrayC));
}

6. Leer por
teclado dos tablas de 12 elementos numéricos y mezclarlas en una tercera de la forma: 3 de la tabla A,
package ejercicio6;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio6 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int arrayA[]= new int[12];
int arrayB[] = new int[12];
int arrayC[] = new int[24];
System.out.println("ingrese los valores de la tabla A");
for(int i=0; i<12; i++){
System.out.print("ingrese el valor "+(i+1)+" : ");
arrayA[i]= teclado.nextInt();
}
System.out.println("ingrese los valores de la tabla B");
for(int i=0; i<12; i++){
System.out.print("ingrese el valor "+(i+1)+" : ");
arrayB[i]= teclado.nextInt();
}
int j=0;
int i=0;
while(i<12){
for(int k=0; k<3; k++){
arrayC[j] = arrayA[i+k];
j++;
}
for(int k=0; k<3; k++){
arrayC[j] = arrayB[i+k];
j++;
}
i+=3;
}
System.out.println("el arreglo resultante es : ");
System.out.println(Arrays.toString(arrayC));
}
}

7. Leer por teclado una serie de 10 números enteros, guardarla en un arreglo. La aplicación debe
indicarnos si los números están ordenados de forma creciente, decreciente, o si están desordenados.
package ejercicio7;

import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio7 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
boolean creciente = false ,decreciente=false;
int array[] = new int[10];
for(int i=0; i<10; i++){
System.out.print("ingrese el valor "+ (i+1)+ " : ");
array[i] = teclado.nextInt();
}
for(int i=0; i<9; i++){
if(array[i]<array[i+1]){
creciente = true;
}
if(array[i]> array[i+1]){
decreciente = true;

}
}
if(creciente==true && decreciente == false){
System.out.println("el areglo esta de forma crecinte");

}
else if(creciente==false && decreciente==true){
System.out.println("el areglo esta de forma decrecinte");
}
else if(creciente==true && decreciente ==true){
System.out.println("el areglo esta desordenado");

}
else if (creciente ==false && decreciente==false){
System.out.println("tosos los numeros son iguales");
}
}

8. Diseñar una aplicación que declare una


tabla de 10 elementos enteros. Leer mediante el teclado 8 números. Después se debe pedir un número
y una posición. El número se debe insertar en la posición indicada, desplazando los que están atrás.
package ejerecicio8;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejerecicio8 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int array[] = new int[10];
int numero,posicion;
for(int i=0;i<8;i++){
System.out.print("INGRESE EL "+ (i)+" NUMERO :"+" ");
array[i]= teclado.nextInt();
}
System.out.print("DIGITE UN NUMERO : ");
numero = teclado.nextInt();
System.out.print("SELECCIONE UNA POSICION : ");
posicion= teclado.nextInt();
for(int i=7; i>=posicion; i--){
array[i+1] = array[i];
}
array[posicion]= numero;
System.out.println( "\n" +Arrays.toString(array));
}
}

9. Crear una aplicación que declare una tabla de 10 elementos enteros. La aplicación debe desplazar la
tabla una posición hacia abajo: el primero pasa a ser el segundo; el segundo, el tercero y así
sucesivamente. El último pasa a ser el primero.
package ejercicio9;
import java.util.Arrays;
import java.util.Scanner;

public class Ejercicio9 {

public static void main(String[] args) {


int a = 0;
Scanner teclado = new Scanner(System.in);
int array[] = new int[10];
for(int i=0; i<10 ; i++){
System.out.print("INGRESA EL NUMERO"+ (i+1)+ " : ");
array[i] = teclado.nextInt();

}
a = array[9];

for(int i=8; i>=0; i--){


array[i+1] = array[i];

}
array[0]=a;
System.out.println(Arrays.toString(array));
}

10. Crear un programa que lea por teclado una tabla de 10 números enteros y desplace N posiciones los
valores del arreglo. (N es digitado por el usuario.)
package ejercicio10;

import java.util.Arrays;
import java.util.Scanner;

public class Ejercicio10 {

public static void main(String[] args) {


int posicionN;
int ultimo;
Scanner teclado = new Scanner(System.in);
int array[] = new int [10];
for(int i=0 ; i<10 ; i++){
System.out.print("digite el numero "+(i+1)+" :");
array[i] = teclado.nextInt();
}
System.out.print("digite la canidad de posiciones a desplazar ");
posicionN = teclado.nextInt();

for(int vuelta=1; vuelta<=posicionN; vuelta++){


ultimo = array[9];
for(int i=8; i>=0; i--){
array[i+1]= array[i];
}
array[0]= ultimo;
}
System.out.println(" EL AREGLO RESULTANTE ES");
System.out.println(Arrays.toString(array));

11. Leer 5 elementos numéricos que se introducirán ordenados de forma creciente. Estos los guardaremos
en una tabla de tamaño 10. Leer un número N, e insertarlo en el lugar adecuado para que la tabla
continúe ordenada.
package ejercicio11;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio11 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int n, sitio = 0, j = 0;
boolean creciente = true;

int array[] = new int[10];


System.out.println("ingresa 5 valores de forma creciente");
do {
for (int i = 0; i < 5; i++) {
System.out.print("ingresa el numero " + (i + 1) + ": ");
array[i] = teclado.nextInt();
}
for (int i = 0; i < 4; i++) {
if (array[i] < array[i + 1]) {
creciente = true;
}
if (array[i] > array[i + 1]) {
creciente = false;
break;
}
}
if (creciente == false) {
System.out.print("los numeros no estan de forma creciente, vuelva a ingresarlos");
}
} while (creciente == false);
System.out.print("ingresa un numero :");
n = teclado.nextInt();

while (array[j] < n && j < 5) {


sitio++;
j++;
}
for (int i = 4; i >= sitio; i--) {
array[i + 1] = array[i];
}
array[sitio] = n;

System.out.println(Arrays.toString(array));

12. Leer por teclado una tabla de 10 elementos numéricos enteros y una posición (entre 0 y 9). Eliminar el
elemento situado en la posición dada sin dejar huecos.
package ejercicio12;

import java.util.Scanner;
/**
*
* @author jeremy
*/
public class Ejercicio12 {

public static void main(String[] args) {


int posicion;
Scanner teclado = new Scanner(System.in);
int array[] = new int[10];
System.out.println("ingresa los valores del arreglo");
for (int i = 0; i < 10; i++) {
System.out.print("digita el numero " + (i) + " : ");
array[i] = teclado.nextInt();

do {
System.out.print("ingresa la posicion que quieras eliminar(0-9) : ");
posicion = teclado.nextInt();
} while (posicion < 0 || posicion > 9);

for (int i = posicion; i < 9; i++) {


array[i] = array[i + 1];
}
for (int i = 0; i < 9; i++) {
System.out.println(" elemento " + array[i]);
}

13. Leer 10 enteros en una


tabla. Guardar en otra tabla los elementos pares de la primera, y a continuación los elementos
package ejercicio13;

import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author jeremy
*/
public class Ejercicio13 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int array[] = new int[10];

int conteo_par = 0;
int conteo_impar = 0;
for (int i = 0; i < 10; i++) {
System.out.print("ingresa el numero " + (i + 1) + " : ");
array[i] = teclado.nextInt();

if (array[i] % 2 == 0) {
conteo_par++;
} else {
conteo_impar++;
}
}
int arrayPar[] = new int[conteo_par];
int arrayImpar[] = new int[conteo_impar];

conteo_par = 0;
conteo_impar = 0;

for (int i = 0; i < 10; i++) {


if (array[i] % 2 == 0) {
arrayPar[conteo_par] = array[i];
conteo_par++;
} else {
arrayImpar[conteo_impar] = array[i];
conteo_impar++;
}
}
System.out.println("arreglo pares");
System.out.println(Arrays.toString(arrayPar));
System.out.println("arreglo impares");
System.out.println(Arrays.toString(arrayImpar));
}

14. Leer 2 series de 10 enteros, que estaban


ordenados crecientemente. Copiar (fusionar)
las dos tablas en una tercera, de forma que sigan ordenados.

package ejercicio14;

import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author jeremy
*/
public class Ejercicio14 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int arrayA[] = new int[10];
int arrayB[] = new int[10];
int arrayC[] = new int[20];
boolean creciente = true;
System.out.println("digite el primer arreglo");

do {

for (int i = 0; i < 10; i++) {


System.out.print("digite el numero" + (i + 1) + " : ");
arrayA[i] = teclado.nextInt();

}
for (int i = 0; i < 9; i++) {
if (arrayA[i] < arrayA[i + 1]) {
creciente = true;
}
if (arrayA[i] > arrayA[i + 1]) {
creciente = false;
break;
}
}
if (creciente == false) {
System.out.println("el arreglo esta desordenado");
}
} while (creciente == false);

System.out.println("digite el segunDo arreglo ");


do {

for (int i = 0; i < 10; i++) {


System.out.print("digite el numero" + (i + 1) + " : ");
arrayB[i] = teclado.nextInt();

}
for (int i = 0; i < 9; i++) {
if (arrayB[i] < arrayB[i + 1]) {
creciente = true;
}
if (arrayB[i] > arrayB[i + 1]) {
creciente = false;
break;
}
}
if (creciente == false) {
System.out.println("el arreglo esta desordenado");
}
} while (creciente == false);
int i = 0;
int j = 0;
int k = 0;

while (i < 10 && j < 10) {


if (arrayA[i] < arrayB[j]) {
arrayC[k] = arrayA[i];
i++;
} else {
arrayC[k] = arrayB[j];
j++;
}
k++;
}
if (i == 10) {
while (j < 10) {
arrayC[k] = arrayB[j];
j++;
k++;
}
} else {
while (i < 10) {
arrayC[k] = arrayA[i];
i++;
k++;

}
}
System.out.println(Arrays.toString(arrayC));
}
}

15. Leer 10 enteros ordenados crecientemente. Leer N y buscar en la tabla. Se debe mostrar la posición en
que se encuentra. Si no está, indicarlo con un mensaje.
package ejercicio15;

import java.util.Scanner;

public class Ejercicio15 {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int array[] = new int[10];
int n;
boolean creciente = true;
do {
for (int i = 0; i < 10; i++) {
System.out.print("ingresa el numero " + (i + 1) + " : ");
array[i] = teclado.nextInt();
}

for (int i = 0; i < 9; i++) {


if (array[i] < array[i + 1]) {
creciente = true;
}
if (array[i] > array[i + 1]) {
creciente = false;
break;
}
}
if (creciente == false) {
System.out.println("el arreglo esta desordenado");
}
} while (creciente == false);

System.out.print("ingresa el numero que quieres buscar ");


n = teclado.nextInt();

int i = 0;
while (i < 10 && array[i] < n) {
i++;
}
if (i == 10) {
System.out.print("el numero no se ha encontrado");

} else {
if (array[i] == n) {
System.out.print("numero encontrado, en la posicion " + i);

} else {
System.out.println("no ha sido encontrado");

}
}
}

16. Queremos desarrollar una aplicación que nos ayude a


gestionar las notas de un centro educativo. Cada
grupo o clase está compuesto por cinco alumnos. Se pide leer las notas del primer, segundo y tercer
trimestre de un grupo. Debemos mostrar al final: la nota media del grupo en cada trimestre y la media
del alumno que se encuentra en la posición N (N se lee por teclado).
package ejercicio16;

import java.util.Scanner;

public class Ejercicio16 {


public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
float primer[], segundo[], tercero[];
float suma1 = 0, suma2 = 0, suma3 = 0, sumaAlum;
float promedio1, promedio2, promedio3, promedioAlum;
int posicion;
primer = new float[5];
segundo = new float[5];
tercero = new float[5];
System.out.println("INGRESE LAS NOTAS DEL PRIMER TRIMESTRE");
for (int i = 0; i < 5; i++) {
System.out.print(" alumno (" + i + "}: ");
primer[i] = teclado.nextInt();
suma1 += primer[i];
}
System.out.println("INGRESE LAS NOTAS DEL SEGUNDO TRIMESTRE");
for (int i = 0; i < 5; i++) {
System.out.print(" alumno (" + i + "}: ");
segundo[i] = teclado.nextInt();
suma2 += segundo[i];
}
System.out.println("INGRESE LAS NOTAS DEL TERCER TRIMESTRE");
for (int i = 0; i < 5; i++) {
System.out.print(" alumno (" + i + "}: ");
tercero[i] = teclado.nextInt();
suma3 += tercero[i];
}
promedio1 = suma1 / 5;
promedio2 = suma2 / 5;
promedio3 = suma3 / 5;

do {
System.out.print("digite la posicion pra sacar el promedio(0-4)");
posicion = teclado.nextInt();
} while (posicion < 0 || posicion > 4);

sumaAlum = primer[posicion] + segundo[posicion] + tercero[posicion];


promedioAlum = sumaAlum / 3;

System.out.println("promedio primer trimestres: " + promedio1);


System.out.println("promedio segundotrimestres: " + promedio2);
System.out.println("promedio tercer trimestres: " + promedio3);
System.out.println("promedo alumno : " + promedioAlum);
}
}
Ordenamiento
Método de burbuja
package metodoburbuja;

import java.util.Arrays;
import java.util.Scanner;

public class MetodoBurbuja {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int array[], nElementos;
System.out.print("ingresa el numero de valores ");
nElementos = teclado.nextInt();
array = new int[nElementos];
for (int i = 0; i < nElementos; i++) {
System.out.print("ingresa el numero" + (i + 1) + " : ");
array[i] = teclado.nextInt();
}
for (int i = 0; i < (nElementos - 1); i++) {
for (int j = 0; j < (nElementos - 1); j++) {
if (array[j] > array[j + 1]) {
int aux = array[j];
array[j] = array[j + 1];
array[j + 1] = aux;
}
}
}
System.out.println("ARREGLO ORDENADO EN FORMA CRECIENTE");
System.out.println(Arrays.toString(array));
System.out.println("ARREGLO ORDENADO EN FORMA DECRECIENTE");
for (int i = (nElementos - 1); i >= 0; i--) {
System.out.print(array[i] + " - ");
}
}

}
Ordenamiento por inserción
package ordenamientoinsercion;

import java.util.Arrays;
import java.util.Scanner;

public class OrdenamientoInsercion {

public static void main(String[] args) {

Scanner teclado = new Scanner(System.in);


int array[], nElementos, posicion = 0, aux = 0;

System.out.print("ingresa el numero de valores ");


nElementos = teclado.nextInt();

array = new int[nElementos];

for (int i = 0; i < nElementos; i++) {


System.out.print("ingresa el numero" + (i + 1) + " : ");
array[i] = teclado.nextInt();
}
for (int i = 0; i < nElementos; i++) {
posicion = i;
aux = array[i];

while ((posicion > 0 && (array[posicion - 1] > aux))) {


array[posicion] = array[posicion - 1];
posicion--;
}
array[posicion] = aux;
}
System.out.println("orden asendentre");
System.out.println(Arrays.toString(array));
System.out.println("orden desendentre");
for (int i = (nElementos - 1); i >= 0; i--) {
System.out.print(array[i] + " - ");
}
}
}
Ordenamiento por selección
package ordenamientoseleccion;

import java.util.Arrays;
import java.util.Scanner;

public class OrdenamientoSeleccion {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int array[], nElementos, min, aux;
System.out.print("ingresa el numero de valores ");
nElementos = teclado.nextInt();

array = new int[nElementos];

for (int i = 0; i < nElementos; i++) {


System.out.print("ingresa el numero " + (i + 1) + " : ");
array[i] = teclado.nextInt();
}
for (int i = 0; i < nElementos; i++) {
min = i;
for (int j = i + 1; j < nElementos; j++) {
if (array[j] < array[min]) {
min = j;

}
}
aux = array[i];
array[i] = array[min];
array[min] = aux;
}
System.out.println("arreglo ordenad crecientemente");
System.out.println(Arrays.toString(array));
System.out.println("arreglo ordenad decrecientemente");
for (int i = (nElementos - 1); i >= 0; i--) {
System.out.print(array[i] + " - ");

}
}
}
Búsqueda
Búsqueda secuencial
package busquedasecuencial;
import java.util.Scanner;
/**
*
* @author jeremy
*/
public class BusquedaSecuencial {

public static void main(String[] args) {


int numero;
int dato;
boolean band = false;
Scanner teclado = new Scanner(System.in);
System.out.print("ingresa cuantos valores vas a ingresar ");
numero = teclado.nextInt();

int array[] = new int[numero];


for (int i = 0; i < numero; i++) {
System.out.print("ingresa el valore " + (i + 1) + " : ");
array[i] = teclado.nextInt();
}

System.out.print("ingresa le numero a buscar ");


dato = teclado.nextInt();

int i = 0;
while (i < numero && band == false) {
if (array[i] == dato) {
band = true;
}
i++;
}
if (band == false) {
System.out.print("no se encuentra el numero");

} else {
System.out.print("Se encuentra en el lugar " + (i - 1) + " .");
}
}

Búsqueda binaria
package busquedabinaria;

import java.util.Scanner;

/**
*
* @author jeremy
*/
public class BusquedaBinaria {

public static void main(String[] args) {


int dato, inf, sup, mitad, i;
boolean band = false;
Scanner teclado = new Scanner(System.in);
int n;
boolean creciente = true;

int array[] = new int[10];


System.out.println("ingresa 5 valores de forma creciente");
do {
for (int j = 0; j < 5; j++) {
System.out.print("ingresa el numero " + (j + 1) + ": ");
array[j] = teclado.nextInt();
}
for (int j = 0; j < 4; j++) {
if (array[j] < array[j + 1]) {
creciente = true;
}
if (array[j] > array[j + 1]) {
creciente = false;
break;
}
}
if (creciente == false) {
System.out.println("los numeros no estan de forma creciente, vuelva a ingresarlos");
}
} while (creciente == false);
System.out.print("ingresa el numero a buscar : ");
dato = teclado.nextInt();

inf = 0;
sup = 5;
i = 0;

mitad = (inf + sup) / 2;


while (inf <= sup && i < 5) {
if (array[mitad] == dato) {
band = true;
break;
}
if (array[mitad] > dato) {
sup = mitad;
mitad = (inf + sup) / 2;
}
if (array[mitad] < dato) {
inf = mitad;
mitad = (inf + sup) / 2;
}
i++;
}
if (band == true) {
System.out.print("el numero esta en la psicion " + mitad + " : ");
} else {
System.out.print("el numero no esta ");

}
}

S-ar putea să vă placă și