import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Intro nº y te calculo su cuadrado:");
do {
try {
n = Integer.parseInt(br.readLine());
if (n == 0) {
System.out.println("Por lógica 0 al cuadrado es 0, intro otro número más lógico...");
}
} catch (IOException | NumberFormatException e) {
System.out.println("El valor ha de ser un número entero");
n = 0;
}
} while (n == 0);
int total = 0;
//SUMAR CON FOR
for (int x = 0; x < n; x++) {
total += n;
}
System.out.println("El cuadrado de " + n + " es " + total);
//RESETEO EL VALOR Y SUMO CON WHILE
total = 0;
int x = 0;
while (x < n) {
total += n;
x++;
}
System.out.println("El cuadrado de " + n + " es " + total);
//RESETEO EL VALOR Y SUMO CON DO WHILE
total = 0;
x = 0;
do {
total += n;
x++;
}
while (x < n);
System.out.println("El cuadrado de " + n + " es " + total);
}
}
[/aux_code]
Calcular cuadrado con sumas
con
No hay comentarios
[aux_code language=»javascript» theme=»tomorrow» title=»Calcular cuadrado con sumas» extra_classes=»»]