Está en la página 1de 7

EJERCICIOS DE PROGRAMACION

1. NUMEROS PRIMOS

package ejercicio1_funciones;

import java.util.Scanner;

public class Ejercicio1_funciones {

public static boolean esPrimo(int numero) {

if (numero <= 1) {

return false;

for (int i = 2; i <= Math.sqrt(numero); i++) {

if (numero % i == 0) {

return false;

return true;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("INGRESE UN NUMERO ENTERO: ");

int numero = scanner.nextInt();

if (esPrimo(numero)) {

System.out.println(" EL NUMERO QUE INGRESO ES UN NUMERO PRIMO ");

} else {

System.out.println(" EL NUMERO QUE INGRESO NO ES UN NUMERO PRIMO ");

}
2. MINIMO COMUN DIVISOR

package ejercicio2_funciones;

import java.util.Scanner;

public class Ejercicio2_funciones {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("INGRESE PRIMER NUMERO: ");

int num1 = scanner.nextInt();

System.out.print("INGRESE SEGUNDO NUMERO: ");

int num2 = scanner.nextInt();

int mcd = calcularMCD(num1, num2);

System.out.println("El MCD de " + num1 + " y " + num2 + " es: " + mcd);

public static int calcularMCD(int a, int b) {

if (b == 0) {

return a;

} else {

return calcularMCD(b, a % b);

}
3. MAYOR A MENOR

package ejercicio3_funciones;

import java.util.Scanner;

public class Ejercicio3_funciones {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("INGRESE EL TAMAÑO QUE DESEA PARA EL ARREGLO: ");

int tamaño = scanner.nextInt();

int[] arreglo = new int[tamaño];

System.out.println("INGRESE LOS NUMEROS PARA SER ASIGNADOS AL ARREGLO:");

for (int i = 0; i < tamaño; i++) {

System.out.print("NUMERO " + (i+1) + ": ");

arreglo[i] = scanner.nextInt();

ordenarArreglo(arreglo);

System.out.println("EL ARRGLO ORDENADO DE MAYOR A MENOR ES LA SIGUIENTE: ");

for (int i = 0; i < tamaño; i++) {

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

public static void ordenarArreglo(int[] arreglo) {


int tamaño = arreglo.length

for (int i = 0; i < tamaño - 1; i++) {

for (int j = 0; j < tamaño - i - 1; j++) {

if (arreglo[j] < arreglo[j+1]) {

int temp = arreglo[j];

arreglo[j] = arreglo[j+1];

arreglo[j+1] = temp;

También podría gustarte