clase13 ejercicios estructurarepetitivafor

5
Ejercicios Estructura repetitiva for Edisson Gutiérrez Jiménez Universidad de Antioquia Lógica y representación I

Upload: adrian-espinosa

Post on 11-Jan-2017

17 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Clase13 ejercicios estructurarepetitivafor

Ejercicios Estructura repetitiva

for

Edisson Gutiérrez Jiménez

Universidad de Antioquia

Lógica y representación I

Page 2: Clase13 ejercicios estructurarepetitivafor

Sintaxis de la estructura de repetición

for en java

⁞ for (var = valorInicial; var <= valorFinal; var = var+paso)

{

% Bloque de Instrucciones

}

Page 3: Clase13 ejercicios estructurarepetitivafor

Ejemplo 1: Algoritmo para calcular el

factorial de un número ingresado por

el usuarioPROGRAMA

int i, num, facto = 1;

Scanner sc = new Scanner( System.in );

System.out.print("Ingrese el número al que desea encontrarle el factorial: ");

num = sc.nextInt();

for (i = 1; i <= num; i++)

{

facto *= i;

}

System.out.println(“El factorial de " + num + " es "+ facto);

Page 4: Clase13 ejercicios estructurarepetitivafor

Ejemplo 2: Prueba de escritorio para el

algoritmo del ejemplo 1

num i facto

4 1 1

2 1

3 2

4 6

5 24

Page 5: Clase13 ejercicios estructurarepetitivafor

Ejercicio 1: ¿Qué hace el siguiente

programa?.

PROGRAMA

int i, x, tope, y, z;

Scanner sc = new Scanner( System.in );

System.out.print(“Ingrese el total de datos: ");

tope = sc.nextInt();

x = 0;

for (i = 1; i<= tope; i++)

{

System.out.print("Ingrese un número ");

z = sc.nextInt();

x += z;

}

y = x / tope;

System.out.println(“Los resultados son " + x + " y "+ y);