Below you will see others 5 Basic exercises solved with conditionals and loops, made in Java language.
6.- Read a number and show its square, repeat the process until a negative number is entered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner;//librería scanner public class Ejercicio6 { public static void main(String[] args) { Scanner teclado = new Scanner( System.in ); // TODO Auto-generated method stub int num,cuadrado; // num guardará el número que leamos // y cuadrado guardará el cuadrado de num System.out.print("Introduzca número: "); num=teclado.nextInt(); while(num>=0){ // repetimos el proceso mientras el número leído no sea negativo cuadrado=num*num; System.out.println(num+ "² es igual a "+ cuadrado); System.out.print("Introduzca otro número: "); num=teclado.nextInt(); // volvemos a leer num } System.out.print("Fin... "); } } |
Example of Results in the Exercise Console 6

7.- Read numbers indicating if it is odd or even. And a 0 it ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.Scanner;//librería scanner public class Ejercicio7 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner teclado = new Scanner( System.in ); int num; System.out.print("Introduzca un número: "); num=teclado.nextInt(); while(num!=0) // mientras num sea distinto de 0 { if(num%2 == 0) // si el resto de dividir entre dos es cero: esto indica que es par System.out.println("Par"); else // en caso contrario: impar System.out.println("Impar"); // repetimos el proceso y volvemos a leer num System.out.print("Introduzca otro número: "); num=teclado.nextInt();; } // salimos del while System.out.println("Fin..."); } } |
Example of Results in the Exercise Console 7

8.- Read numbers and add them until a 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner;//librería scanner public class Ejercicio8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner teclado = new Scanner( System.in ); int num,suma; suma=0; do { System.out.print("Introduzca un número: "); num=teclado.nextInt(); suma=suma+num; } while(num!=0); System.out.println("La suma de todos los números es: "+suma); } } |
Example of Results in the Exercise Console 8

9.- Read numbers and calculate the mean. The process ends when reading a negative number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.Scanner; public class Ejercicio9 { public static void main(String[] args) { Scanner teclado = new Scanner( System.in ); int num, suma, elementos; float media; // la media puede tener decimales // num: guardará los números introducidos por el usuario // suma: almacenará la suma de todos los números introducidos // elementos: será un contador que indicará el números de números 8o elementos) introducidos System.out.print("Introduzca un número: "); num=teclado.nextInt(); suma= 0; elementos= 0; while(num>=0) // nos interesan los positivos y el cero { suma+=num; elementos++; System.out.print("Introduzca otro número: "); num=teclado.nextInt(); } if (elementos == 0) // daría un error de división por cero System.out.println("Imposible hacer la media"); else { media= (float)suma/elementos; System.out.println("La media es de: " + media); } } } |
Example of Results in the Exercise Console 9

10.- Read a number N and show the numbers of 1 a N.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; public class Ejercicio10 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner teclado = new Scanner( System.in ); int i,num; System.out.print("Introduce un número: "); num=teclado.nextInt(); i=1; // i es el contador que tomará los valores de 1 a n while(i<=num){ System.out.println(i); i++; } } } |
Example of Results in the Exercise Console 10

Let's hope and these examples have been of great help.
Do not forget to follow us on social networks, to be aware of everything.
Follow Facialix on all his social networks:
- 100% Discount on RaspBerry Pi Course - 10 April, 2021
- Cybersecurity: Training, Talent Promotion And Research - 10 April, 2021
- How to Develop Mobile Video Games - 10 April, 2021