Documente Academic
Documente Profesional
Documente Cultură
ESTRUCTURAS REPETITIVAS I
Las estructuras repetitivas se pueden representar por: while, do/while y/o for.
Escribir: Num do { System.out.print(“\t”+Num);
c = c +1; // también podría ser c++;
c = c+1 }
while ( c < N) ;
c < N
} // llave fin de main
Fin } // llave fin de la clase
1
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
} // llave fin de main
} // llave fin de la clase
Fin
a) solución con: while
import java.io.*;
public class repetitiva2a
Inicio { public static void main(String[]args) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N, Num, c=0,s=0 int N, Num, c=0, s=0;
do{ System.out.print(“Ingrese cantidad de numeros :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0) ; // do while termina en punto y coma
c < N
while( c < N) // while NO termina en punto y coma
Leer Num { System.out.print(“Ingrese numero : “ );
Num= Integer.parseInt(br.readLine());
c = c + 1 c = c + 1;
s = s + Num;
s = s + Num }
2
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
2b.Leer N númer os enter os y r epor tar su suma
b) solución con: do/while
import java.io.*;
public class repetitiva2b
Inicio { public static void main(String[]args) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N, Num, c=0,s=0 int N, Num, c=0, s=0;
do{ System.out.print(“Ingrese cantidad de numeros :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0) ; // do while termina en punto y coma
Leer Num
do { System.out.print(“Ingrese numero : “ );
c = c + 1 Num= Integer.parseInt(br.readLine());
c = c + 1;
s = s + Num s = s + Num;
}
c < N while (c < N);
c) solución con: for
import java.io.*;
public class repetitiva2c
Inicio { public static void main(String[]args) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N, Num, c,s=0 int N, Num, c, s=0;
do{ System.out.print(“Ingrese cantidad de numeros :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0) ; // do while termina en punto y coma
c=0; c < N ; c=c+1
for ( c=0; c<N ; c = c+1)
Leer Num { System.out.print(“Ingrese numero : “ );
Num= Integer.parseInt(br.readLine());
s = s + Num s = s + Num;
}
3
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
3a.Leer N númer os r eales positivos y r epor tar su pr omedio.
impor t java.io.*;
a) solución con: while
public class r epetitiva3a
{ public static void main(Str ing[]ar gs) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, c=0;
Variables N, Num, c=0,s=0,pr double Num, s=0, pr;
do{ System.out.print(“Ingrese cantidad de números :”);
Leer N N= Integer.parseInt(br.readLine());
}
N ≤ 0 while (N <=0) ; // do while termina en punto y coma
c < N while( c < N) // while NO termina en punto y coma
{ do { System.out.print(“Ingrese número : “ );
Leer Num
Num=Double.parseDouble(br.readLine());
Num ≤ 0 }
while ( Num <=0);
c = c + 1
c ++; // c++ equivale a decir c = c+1
s = s + Num s = s + Num;
}
pr = s / N pr = s / N;
System.out.println (“El promedio es = “ + pr );
escribir : pr } // llave fin de main
Fin } // llave fin de la clase
s = s + Num s = s + Num;
}
c < N while( c < N) ;
pr = s / N;
pr = s / N System.out.println (“El promedio es = “ + pr );
escribir : pr } // llave fin de main
Fin } // llave fin de la clase
4
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
3c.Leer N númer os reales positivos y r eportar su pr omedio.
a) solución con: for
impor t java.io.*;
public class r epetitiva3c
Inicio { public static void main(Str ing[]ar gs) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Variables N, Num, c,s=0,pr int N, c;
double Num, s=0, pr;
Leer N do{ System.out.print(“Ingrese cantidad de números :”);
N= Integer.parseInt(br.readLine());
N ≤ 0 }
while (N <=0) ; // do while termina en punto y coma
c =0 ; c < N ; c = c + 1
for ( c=0 ; c<N ; c ++ ) // c++ equivale a decir c = c+1
{ do { System.out.print(“Ingrese número : “ );
Leer Num
Num=Double.parseDouble(br.readLine());
Num ≤ 0 }
while ( Num <=0);
s = s + Num s = s + Num;
}
pr = s / N pr = s / N;
System.out.println (“El promedio es = “ + pr );
escribir : pr } // llave fin de main
} // llave fin de la clase
Fin
5
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
4a.Leer las edades de N personas y reportar la suma de las edades y la edad promedio.
N , ed sum, pro
Solución : usando while
import java.io.*;
public class repetitiva4a
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, ed, c=0, sum=0;
Variables N, ed, c=0, sum=0, pro double pro;
do{ System.out.print(“Ingrese No. de datos :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0); // do while termina en punto y coma
c < N while ( c < N) // while solo, no usa punto y coma
{ do { System.out.print(“Ingrese edad : ”);
Leer ed ed= Integer.parseInt(br.readLine());
}
ed ≤ 0 while (ed <=0);
c = c +1 c = c + 1;
sum = sum + ed;
sum = sum + ed }
pro = sum / N;
pro = sum /N System.out.println(“La suma de edades es = ”+ sum);
System.out.println(“Promedio de las edades es= ”+ pro);
Escribir : sum , pro
} // llave fin de main
Fin
} // llave fin de la clase
6
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
4b.Leer las edades de N personas y reportar la suma de las edades y la edad promedio.
N , ed sum, pro
Solución: usando do/ while
import java.io.*;
public class repetitiva4b
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, ed, c=0, sum=0;
Variables N, ed, c=0, sum=0, pro double pro;
do{ System.out.print(“Ingrese No. de datos :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0); // do while termina en punto y coma
Leer ed do { do { System.out.print(“Ingrese edad : ”);
ed= Integer.parseInt(br.readLine());
ed ≤ 0 }
while (ed <=0);
c = c +1 c = c + 1;
sum = sum + ed;
sum = sum + ed
}
c < N while ( c < N); // do while termina punto y coma
pro = sum / N;
pro = sum /N
System.out.println(“La suma de edades es = ”+ sum);
Escribir : sum , pro System.out.println(“Promedio de las edades es= ”+ pro);
Fin
} // llave fin de main
} // llave fin de la clase
7
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
4c.Leer las edades de N personas y reportar la suma de las edades y la edad promedio.
N , ed sum, pro
Solución: usando for
import java.io.*;
public class repetitiva4c
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, ed, c, sum=0;
Variables N, ed, c, sum=0, pro double pro;
do{ System.out.print(“Ingrese No. de datos :”);
Leer N
N= Integer.parseInt(br.readLine());
}
N ≤ 0
while (N <=0); // do while termina en punto y coma
c =0; c < N ; c =c+1 for ( c=0 ; c<N ; c = c+1 )
{ do { System.out.print(“Ingrese edad : ”);
Leer ed ed= Integer.parseInt(br.readLine());
}
ed ≤ 0 while (ed <=0);
sum = sum + ed sum = sum + ed;
}
pro = sum / N;
pro = sum /N
System.out.println(“La suma de edades es = ”+ sum);
Escribir : sum , pro System.out.println(“El promedio de las edades es= ”+
pro);
Fin
} // llave fin de main
} // llave fin de la clase
8
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
5a.Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad
de desaprobados (cde).
Solución: usando while
import java.io.*;
public class repetitiva5a
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, not, c=0, cap=0, cde=0;
Variables N,not,c=0,cap=0,cde=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
while ( c < N) // while solo, no usa punto y coma
c < N {
do { System.out.print(“Ingrese nota : ”);
Leer not not= Integer.parseInt(br.readLine());
}
not < 0 V not > 20 while (not < 0 || not > 20);
c = c + 1;
c = c +1
if( not >=11)
v not ≥ 11 F { cap = cap + 1;
}
cap = cap + 1 cde=cde+1 else { cde = cde + 1;
}
Escribir : cap , cde }
System.out.println(“Cantidad de aprobados = ”+ cap);
Fin
System.out.println(“Cantidad de desaprobados= ”+ cde);
} // llave fin de main
} // llave fin de la clase
9
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
5b.Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad
de desaprobados (cde).
Solución: usando do while
import java.io.*;
public class repetitiva5b
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, not, c=0, cap=0, cde=0;
Variables N,not,c=0,cap=0,cde=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
do { do{ System.out.print(“Ingrese nota : ”);
Leer not not= Integer.parseInt(br.readLine());
}
not < 0 V not > 20 while (not < 0 || not > 20);
c = c + 1;
c = c +1 if( not >=11)
{ cap = cap + 1;
v not ≥ 11 F }
else { cde = cde + 1;
cap = cap + 1 cde=cde+1 }
}
c < N while ( c < N) ;
System.out.println(“Cantidad de aprobados = ”+ cap);
Escribir : cap , cde System.out.println(“Cantidad de desaprobados= ”+ cde);
} // llave fin de main
Fin
} // llave fin de la clase
10
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
5c.Leer la nota de N alumnos y reportar la cantidad de aprobados (cap)y la cantidad
de desaprobados (cde).
Solución: usando for
import java.io.*;
public class repetitiva5c
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, not, c , cap=0, cde=0;
Variables N,not,c ,cap=0,cde=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
11
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
6.Leer la edad de N alumnos y reportar la cantidad de mayores de edad (cma)y la
cantidad de menores de edad (cme).
Solución: usando for
import java.io.*;
public class repetitiva6
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, ed, c , cma=0, cme=0;
Variables N,ed, c ,cma=0,cme=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
12
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
7. 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).
Solución: usando for
import java.io.*;
public class repetitiva7
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, not, c , sap=0, sde=0;
Variables N,not, c ,sap=0,sde=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
13
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
8. Leer el sexo de N alumnos y reportar la cantidad de hombres (ch) y la cantidad
de mujeres (cm).
import java.io.*;
Solución: usando for public class repetitiva8
{ public static void main(String[]args) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Inicio int N, c, ch=0, cm=0;
char sex;
Variables N, sex, c ,ch=0,cm=0 String cad;
do{ System.out.print(“Ingrese No. de alumnos :”);
Leer N N= Integer.parseInt(br.readLine());
}
N ≤ 0 while (N <=0); // do while termina en punto y coma
for ( c=0 ; c<N ; c = c+1 )
c = 0 ; c < N ; c = c+1 { do { System.out.print(“Ingrese sexo : ”);
cad=br.readLine();
Leer sex cad=cad.toUpperCase();
sex= cad.charAt(0);
sex ≠ ‘M’ Λ sex ≠ ‘F’ }
while (sex != ’M’ && sex != ‘F’);
v sex=’M’ F
if( sex ==’M’)
{ ch ++; // ch ++ es igual a decir ch=ch+1
ch = ch + 1 cm = cm + 1
}
else { cm ++; // cm ++ es igual a decir cm=cm+1
}
Escribir : ch , cm }
System.out.println(“Cantidad de hombres = ”+ ch);
Fin System.out.println(“Cantidad de mujeres = ”+ cm);
} // llave fin de main
} // llave fin de la clase
14
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
9. Leer nota y edad de N alumnos y reportar:
a. Cantidad aprobados mayores de edad (apm)
b. Cantidad de desaprobados menores de edad (dme)
c. Suma de las notas de los menores de edad (snm)
N , not, ed apm, dme, snm
Solución: usando for
import java.io.*;
Inicio
public class repetitiva9
{ public static void main(String[]args) throws IOException
Variables N, not, ed, c , apm=0
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
dme=0, snm=0 int N, not, ed,c, apm=0, dme, snm;
do{ System.out.print(“Ingrese No. de alumnos :”);
Leer N N= Integer.parseInt(br.readLine());
}
N ≤ 0 while (N <=0); // do while termina en punto y coma
for ( c=0 ; c<N ; c = c+1 )
c = 0 ; c < N ; c = c+1 { do { System.out.print(“Ingrese nota : ”);
not =Integer.parseInt(br.readLine());
Leer not }
while (not<0|| not>20);
not <0 V not > 20 do { System.out.print(“Ingrese edad : ”);
ed =Integer.parseInt(br.readLine());
Leer ed }
while (ed <=0);
ed ≤ 0
if( not>=11 && ed >=18)
V not≥ 11 Λ ed≥ 18 F
{ apm ++;
}
apm = apm +1
15
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ESTRUCTURA REPETITIVA I
INTRODUCCIÓN A LA PROGRAMACIÓN PROBLEMAS RESUELTOS
10.Leer N números enteros positivos y reportar la cantidad de pares (cp)y la cantidad
de impares. (ci).
Solución: usando while
import java.io.*;
public class repetitiva10
{ public static void main(String[]args) throws IOException
Inicio {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N, Num, c=0, cp=0, ci=0;
Variables N, Num, c=0,cp=0,ci=0 do{ System.out.print(“Ingrese No. de alumnos :”);
N= Integer.parseInt(br.readLine());
Leer N
}
while (N <=0); // do while termina en punto y coma
N ≤ 0
while ( c < N) // while solo, no usa punto y coma
c < N {
do { System.out.print(“Ingrese Numero : ”);
Leer Num Num= Integer.parseInt(br.readLine());
}
Num ≤ 0 while (Num <= 0);
c = c + 1;
c = c +1
if( Num%2 == 0)
v Num%2=0 F { cp = cp + 1;
}
cp = cp + 1 ci = ci + 1 else { ci = ci + 1;
}
Escribir : cp , ci }
System.out.println(“Cantidad de pares = ”+ cp);
Fin
System.out.println(“Cantidad de impares= ”+ ci);
} // llave fin de main
} // llave fin de la clase
16