UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION
INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
1
ESTRUCTURAS REPETITIVAS
Las estructuras repetitivas se pueden representar por: while, do/while y/o for. 1a.-Leer N nmeros enteros y reportar su suma. a) solucin con: while
1b.-Leer N nmeros enteros y reportar su suma b) solucin con: do/while
import java.io.*; public class repetitiva2a { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, Num, c=0, s=0; do{ System.out.print(Ingrese cantidad de numeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma
while( c < N) // while NO termina en punto y coma { System.out.print(Ingrese numero : ); Num= Integer.parseInt(br.readLine()); c = c + 1; s = s + Num; }
System.out.println(Suma de los nmeros = + s ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c=0,s=0 Leer N N 0 c < N Leer Num c = c + 1 s = s + Num escribir : s Fin
import java.io.*; public class repetitiva2b { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, Num, c=0, s=0; do{ System.out.print(Ingrese cantidad de numeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma
do { System.out.print(Ingrese numero : ); Num= Integer.parseInt(br.readLine()); c = c + 1; s = s + Num; } while (c < N);
System.out.println(Suma de los nmeros = + s ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c=0,s=0 Leer N N 0 Leer Num c = c + 1 s = s + Num c < N escribir : s Fin
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
2
1c.-Leer N nmeros enteros y reportar su suma c) solucin con: for
2a.-Leer N nmeros reales positivos y reportar su promedio. a) solucin con: while
import java.io.*; public class repetitiva2c { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, Num, c, s=0; do{ System.out.print(Ingrese cantidad de numeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma
for ( c=0; c<N ; c = c+1) { System.out.print(Ingrese numero : ); Num= Integer.parseInt(br.readLine()); s = s + Num; }
System.out.println(Suma de los nmeros = + s ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c,s=0 Leer N N 0 c=0; c < N ; c=c+1 Leer Num s = s + Num
escribir : s Fin
import java.io.*; public class repetitiva3a { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, c=0; double Num, s=0, pr; do{ System.out.print(Ingrese cantidad de nmeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma
while( c < N) // while NO termina en punto y coma { do { System.out.print(Ingrese nmero : ); Num=Double.parseDouble(br.readLine()); } while ( Num <=0);
c ++; // c++ equivale a decir c = c+1 s = s + Num; } pr = s / N; System.out.println (El promedio es = + pr ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c=0,s=0,pr Leer N N 0 c < N Leer Num Num 0 c = c + 1 s = s + Num pr = s / N escribir : pr Fin
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
3
2b) solucin con: do/ while
2c.-Leer N nmeros reales positivos y reportar su promedio. a) solucin con: for
import java.io.*; public class repetitiva3b { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, c=0; double Num, s=0, pr; do{ System.out.print(Ingrese cantidad de nmeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma do { do { System.out.print(Ingrese nmero : ); Num=Double.parseDouble(br.readLine()); } while ( Num <=0); c ++; // c++ equivale a decir c = c+1
s = s + Num; } while( c < N) ; pr = s / N; System.out.println (El promedio es = + pr ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c=0,s=0,pr Leer N N 0 Leer Num Num 0 c = c + 1 s = s + Num c < N pr = s / N escribir : pr Fin
import java.io.*; public class repetitiva3c { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, c; double Num, s=0, pr; do{ System.out.print(Ingrese cantidad de nmeros :); N= Integer.parseInt(br.readLine()); } while (N <=0) ; // do while termina en punto y coma for ( c=0 ; c<N ; c ++ ) // c++ equivale a decir c = c+1 { do { System.out.print(Ingrese nmero : ); Num=Double.parseDouble(br.readLine()); } while ( Num <=0); s = s + Num; }
pr = s / N; System.out.println (El promedio es = + pr ); } // llave fin de main } // llave fin de la clase Inicio Variables N, Num, c,s=0,pr Leer N N 0 c =0 ; c < N ; c = c + 1 Leer Num Num 0 s = s + Num
pr = s / N escribir : pr Fin
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
4
3a.-Leer las edades de N personas y reportar la suma de las edades y la edad promedio. Entrada Salida N , ed sum, pro
Solucin : usando while
VALIDAR PROCESO Inicio Variables N, ed, c=0, sum=0, pro Leer N N 0 c < N Leer ed ed 0 c = c +1 sum = sum + ed pro = sum /N Escribir : sum , pro Fin
import java.io.*; public class repetitiva4a { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, ed, c=0, sum=0; double pro; do{ System.out.print(Ingrese No. de datos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma while ( c < N) // while solo, no usa punto y coma { do { System.out.print(Ingrese edad : ); ed= Integer.parseInt(br.readLine()); } while (ed <=0);
c = c + 1; sum = sum + ed; } pro = sum / N; System.out.println(La suma de edades es = + sum); System.out.println(Promedio de las edades es= + pro);
} // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
5
3b.-Leer las edades de N personas y reportar la suma de las edades y la edad promedio. Entrada Salida N , ed sum, pro
Solucin: usando do/ while
VALIDAR PROCESO Inicio Variables N, ed, c=0, sum=0, pro Leer N N 0 Leer ed ed 0 c = c +1 sum = sum + ed c < N pro = sum /N Escribir : sum , pro Fin
import java.io.*; public class repetitiva4b { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, ed, c=0, sum=0; double pro; do{ System.out.print(Ingrese No. de datos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma do { do { System.out.print(Ingrese edad : ); ed= Integer.parseInt(br.readLine()); } while (ed <=0); c = c + 1; sum = sum + ed;
} while ( c < N); // do while termina punto y coma pro = sum / N;
System.out.println(La suma de edades es = + sum); System.out.println(Promedio de las edades es= + pro);
} // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
6
3c.-Leer las edades de N personas y reportar la suma de las edades y la edad promedio. Entrada Salida N , ed sum, pro
Solucin: usando for
VALIDAR PROCESO Inicio Variables N, ed, c, sum=0, pro Leer N N 0 c =0; c < N ; c =c+1 Leer ed ed 0 sum = sum + ed
pro = sum /N Escribir : sum , pro Fin
import java.io.*; public class repetitiva4c { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, ed, c, sum=0; double pro; do{ System.out.print(Ingrese No. de datos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma for ( c=0 ; c<N ; c = c+1 ) { do { System.out.print(Ingrese edad : ); ed= Integer.parseInt(br.readLine()); } while (ed <=0);
sum = sum + ed; }
pro = sum / N;
System.out.println(La suma de edades es = + sum); System.out.println(El promedio de las edades es= + pro);
} // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
7
4a.-Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad de desaprobados (cde). Entrada Salida N , not cap, cde
Solucin: usando while
VALIDAR PROCESO Inicio Variables N,not,c=0,cap=0,cde=0 Leer N N 0 c < N Leer not not < 0 V not > 20 c = c +1 v not 11 F cap = cap + 1 cde=cde+1 Escribir : cap , cde Fin
import java.io.*; public class repetitiva5a { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, not, c=0, cap=0, cde=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma while ( c < N) // while solo, no usa punto y coma { do { System.out.print(Ingrese nota : ); not= Integer.parseInt(br.readLine()); } while (not < 0 || not > 20); c = c + 1;
if( not >=11) { cap = cap + 1; } else { cde = cde + 1; } } System.out.println(Cantidad de aprobados = + cap); System.out.println(Cantidad de desaprobados= + cde); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
8
4b.-Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad de desaprobados (cde). Entrada Salida N , not cap, cde
Solucin: usando do while
VALIDAR PROCESO Inicio Variables N,not,c=0,cap=0,cde=0 Leer N N 0 Leer not not < 0 V not > 20 c = c +1 v not 11 F cap = cap + 1 cde=cde+1 c < N Escribir : cap , cde Fin
import java.io.*; public class repetitiva5b { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, not, c=0, cap=0, cde=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma do { do{ System.out.print(Ingrese nota : ); not= Integer.parseInt(br.readLine()); } while (not < 0 || not > 20); c = c + 1; if( not >=11) { cap = cap + 1; } else { cde = cde + 1; } } while ( c < N) ; System.out.println(Cantidad de aprobados = + cap); System.out.println(Cantidad de desaprobados= + cde); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
9
4c.-Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad de desaprobados (cde). Entrada Salida N , not cap, cde
Solucin: usando for
VALIDAR PROCESO Inicio Variables N,not,c ,cap=0,cde=0 Leer N N 0 c = 0 ; c < N ; c = c+1 Leer not not < 0 V not > 20 v not 11 F cap = cap + 1 cde=cde+1
Escribir : cap , cde Fin
import java.io.*; public class repetitiva5c { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, not, c , cap=0, cde=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma
for ( c=0 ; c<N ; c = c+1 ) { do { System.out.print(Ingrese nota : ); not= Integer.parseInt(br.readLine()); } while (not < 0 || not > 20); if( not >=11) { cap = cap + 1; } else { cde = cde + 1; } } System.out.println(Cantidad de aprobados = + cap); System.out.println(Cantidad de desaprobados= + cde); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
10
5.-Leer la edad de N alumnos y reportar la cantidad de mayores de edad (cma)y la cantidad de menores de edad (cme). Entrada Salida N , ed cma, cme
Solucin: usando for
VALIDAR PROCESO Inicio Variables N,ed, c ,cma=0,cme=0 Leer N N 0 c = 0 ; c < N ; c = c+1 Leer ed ed 0 v ed 18 F cma = cma + 1 cme=cme+1
Escribir : cma , cme Fin
import java.io.*; public class repetitiva6 { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, ed, c , cma=0, cme=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma
for ( c=0 ; c<N ; c = c+1 ) { do { System.out.print(Ingrese edad : ); ed= Integer.parseInt(br.readLine()); } while ( ed <= 0); if( ed >=11) { cma = cma + 1; } else { cme = cme + 1; } } System.out.println(Cantidad de mayores de edad=+cma); System.out.println(Cantidad de menores de edad=+cme); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
11
6.- Leer la nota de N alumnos y reportar la suma de las notas de los aprobados (sap) y la suma de las notas de los desaprobados (sde). Entrada Salida N , not sap, sde
Solucin: usando for
VALIDAR PROCESO Inicio Variables N,not, c ,sap=0,sde=0 Leer N N 0 c = 0 ; c < N ; c = c+1 Leer not not < 0 V not > 20 v not 11 F sap = sap + not sde=sde + not
Escribir : sap , sde Fin
import java.io.*; public class repetitiva7 { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, not, c , sap=0, sde=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma
for ( c=0 ; c<N ; c = c+1 ) { do { System.out.print(Ingrese nota : ); not= Integer.parseInt(br.readLine()); } while (not < 0 || not > 20); if( not >=11) { sap = sap + not; } else { sde = sde + not; } } System.out.println(Suma notas aprobados = + sap); System.out.println(Suma notas desaprobados= + sde); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
12
7.- Leer el sexo de N alumnos y reportar la cantidad de hombres (ch) y la cantidad de mujeres (cm). Entrada Salida N , sex ch, cm
Solucin: usando for
VALIDAR PROCESO Inicio Variables N, sex, c ,ch=0,cm=0 Leer N N 0 c = 0 ; c < N ; c = c+1 Leer sex sex M sex F v sex=M F ch = ch + 1 cm = cm + 1
Escribir : ch , cm Fin
import java.io.*; public class repetitiva8 { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, c, ch=0, cm=0; char sex; String cad; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma for ( c=0 ; c<N ; c = c+1 ) { do { System.out.print(Ingrese sexo : ); cad=br.readLine(); cad=cad.toUpperCase(); sex= cad.charAt(0); } while (sex != M && sex != F); if( sex ==M) { ch ++; // ch ++ es igual a decir ch=ch+1 } else { cm ++; // cm ++ es igual a decir cm=cm+1 } } System.out.println(Cantidad de hombres = + ch); System.out.println(Cantidad de mujeres = + cm); } // llave fin de main } // llave fin de la clase
UNIVERSIDAD NACIONAL DE TRUJILLO LENGUAJES DE PROGRAMACION INGENIERA DE MINAS ING. SILVIA RODRIGUEZ AGUIRRE
13
8.-Leer N nmeros enteros positivos y reportar la cantidad de pares (cp)y la cantidad de impares. (ci). Entrada Salida N , Num cp, ci
Solucin: usando while
VALIDAR PROCESO Inicio Variables N, Num, c=0,cp=0,ci=0 Leer N N 0 c < N Leer Num Num 0 c = c +1 v Num%2=0 F cp = cp + 1 ci = ci + 1 Escribir : cp , ci Fin
import java.io.*; public class repetitiva10 { public static void main(String[]args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N, Num, c=0, cp=0, ci=0; do{ System.out.print(Ingrese No. de alumnos :); N= Integer.parseInt(br.readLine()); } while (N <=0); // do while termina en punto y coma while ( c < N) // while solo, no usa punto y coma { do { System.out.print(Ingrese Numero : ); Num= Integer.parseInt(br.readLine()); } while (Num <= 0); c = c + 1;
if( Num%2 == 0) { cp = cp + 1; } else { ci = ci + 1; } } System.out.println(Cantidad de pares = + cp); System.out.println(Cantidad de impares= + ci); } // llave fin de main } // llave fin de la clase