Bucles 1-9
This commit is contained in:
1 parent
cf7bb1f1b9
commit
2e8c5cae24
10 files changed
+441770
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
// Diseña un algoritmo que visualice los 50 primeros números enteros.
|
||||||
|
void main() {
|
||||||
|
int i = -1;
|
||||||
|
while (++i < 50)
|
||||||
|
System.out.print(i + ", ");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// Diseña un algoritmo que sume los números comprendidos entre el 1 y un número introducido
|
||||||
|
// por el usuario a través del teclado. Si el número introducido es <1 la suma será 0.
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
System.out.println("Introduce un número:");
|
||||||
|
long resultado = 0;
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
if (n >= 1) {
|
||||||
|
int i = 0;
|
||||||
|
while (++i <= n)
|
||||||
|
resultado += i;
|
||||||
|
} else
|
||||||
|
resultado = 0;
|
||||||
|
|
||||||
|
System.out.printf("El resultado es %d\n", resultado);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// Diseña un algoritmo que muestre los números comprendidos entre dos números
|
||||||
|
// introducidos desde el teclado en orden ascendente
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
System.out.println("Introduce dos números:");
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n1 = sc.nextInt();
|
||||||
|
int n2 = sc.nextInt();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
while(n1 <= n2)
|
||||||
|
System.out.printf("%d, ", n1++);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// Diseño de un algoritmo que sume todos aquellos números leídos mientras no sean negativos,
|
||||||
|
// al introducirse un número negativo, muestra la suma y acaba el programa
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int resultado = 0;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
System.out.print("Introduce un número: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
if (n < 0)
|
||||||
|
break;
|
||||||
|
resultado += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.close();
|
||||||
|
System.out.printf("El resultado es: %d\n", resultado);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// Diseña un algoritmo que calcule y muestre el factorial de un número entero positivo dado
|
||||||
|
// por teclado
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
System.out.println("Calcular factorial de: ");
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
long resultado = 1;
|
||||||
|
while (n > 0)
|
||||||
|
resultado *= n--;
|
||||||
|
|
||||||
|
System.out.printf("El resultado es: %d\n", resultado);
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// Diseñar un algoritmo correspondiente a un programa que lee un número entero positivo y
|
||||||
|
// determina el número de dígitos decimales necesarios para la representación de dicho valor
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
System.out.println("Introduce un número positivo: ");
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
int resultado = 0;
|
||||||
|
while (n > 0) {
|
||||||
|
n /= 10;
|
||||||
|
resultado++;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.printf("El resultado es %d\n", resultado);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// Diseña un algoritmo que lea 100 números y cuente los positivos, negativos y nulos y muestre
|
||||||
|
// los totales
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int positivos = 0, negativos = 0, nulos = 0;
|
||||||
|
System.out.println("Introduce 100 numeros: ");
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int i = 0;
|
||||||
|
while (i++ < 100) {
|
||||||
|
int n = sc.nextInt();
|
||||||
|
if (n > 0)
|
||||||
|
positivos++;
|
||||||
|
else if (n < 0) {
|
||||||
|
negativos++;
|
||||||
|
} else
|
||||||
|
nulos++;
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
System.out.printf("positivos: %d\nnegativos: %d\nnulos: %d\n", positivos, negativos, nulos);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// Diseña un algoritmo que calcule la potencia de un número por productos sucesivos dada la
|
||||||
|
// base y el exponente
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Introduce la base: ");
|
||||||
|
long base = sc.nextLong();
|
||||||
|
System.out.println("Introduce el exponente: ");
|
||||||
|
long exponente = sc.nextLong();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
long resultado = 1;
|
||||||
|
while (exponente-- > 0)
|
||||||
|
resultado *= base;
|
||||||
|
|
||||||
|
System.out.printf("resultado: %d\n", resultado);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Diseñar un algoritmo que calcule y muestre la suma de números introducidos por teclado
|
||||||
|
// hasta que dicha suma sea mayor que 1000
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
long suma = 0;
|
||||||
|
while (suma < 1000) {
|
||||||
|
System.out.println("Introduce un número");
|
||||||
|
suma += sc.nextLong();
|
||||||
|
System.out.printf("suma: %d\n", suma);
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user