Sunteți pe pagina 1din 16

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 

ESTRUCTURAS REPETITIVAS I 

Las estructuras repetitivas se pueden representar por:  while,  do/while  y/o  for. 

1a.­Leer  un númer o enter o Num  y escribir lo  N veces 

Solución usando:  while  import java.io.*; 


public class  repetitiva1 
Inicio  { public static void main(String[]args) throws IOException 
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
Variables   N, Num, c = 0  int  N, Num,  c=0; 
System.out.print(“Ingrese numero :”); 
Leer  Num  Num = Integer.parseInt(br.readLine()); 
do{ System.out.print(“Ingrese cantidad de veces :”); 
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 sólo, no usa punto y coma 
Escribir:  Num  { System.out.print(“\t”+Num); 
c = c +1;  //   c=c+1  Equivale a decir  c++ 
c = c+1  }  // se puede utilizar:  c=c+1   ó  c++ 
}  // llave fin de main 
Fin  }   // llave  fin de la clase 

1b.­Leer  un númer o enter o  Num  y escribir lo  N veces 

Solución con:  do/while  import java.io.*; 


public class  repetitiva1b 
Inicio  { public static void main(String[]args) throws IOException 
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
Variables   N, Num, c = 0  int  N, Num,  c=0; 
System.out.print(“Ingrese numero :”); 
Leer  Num  Num = Integer.parseInt(br.readLine()); 
do{ System.out.print(“Ingrese cantidad de veces :”); 
Leer N  N= Integer.parseInt(br.readLine()); 

N  ≤    0  while (N <=0);     // do while termina en punto y coma 

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 


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 

1c.­Leer un númer o entero  Num  y escr ibir lo  N veces 

Solución usando:  for   import java.io.*; 


public class  repetitiva1c 
Inicio  { public static void main(String[]args) throws IOException 
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
Variables   N, Num, c  int  N, Num,  c; 
System.out.print(“Ingrese numero :”); 
Leer  Num  Num = Integer.parseInt(br.readLine()); 
do{ System.out.print(“Ingrese cantidad de veces :”); 
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)  // c=c+1 equivale a decir  c++ 


{  System.out.print(“\t”+Num); 
Escribir:  Num  } 

}  // llave fin de main 
}   // llave  fin de la clase 
Fin 

2a.­Leer  N númer os enter os y r epor tar  su suma. 

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  }

escribir  :   s  System.out.println(“Suma de los  números  = “ +  s ); 


}  // llave fin de main 
Fin }   // llave  fin de la clase 


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); 

escr ibir  :   s  System.out.println(“Suma de los  números  = “ +  s ); 


}  // llave fin de main 
Fin  }   // llave  fin de la clase 

2c.­Leer N númer os enteros y r eportar su suma 

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; 

escribir  :   s  System.out.println(“Suma de los  números  = “ +  s ); 


}  // llave fin de main 
Fin }   // llave  fin de la clase 


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 

b) solución con:  do/ while  impor t java.io.*; 


public class  r epetitiva3b 
{ 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 
Leer  Num  do {  do { System.out.print(“Ingrese  número : “ ); 
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; 

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 


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


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. 

Entrada  VALIDAR  PROCESO  Salida 

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


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. 

Entrada  VALIDAR  PROCESO  Salida 

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


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. 

Entrada  VALIDAR  PROCESO  Salida 

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


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). 

Entrada  VALIDAR  PROCESO  Salida 

N , not  cap,  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


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). 

Entrada  VALIDAR  PROCESO  Salida 

N , not  cap,  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). 

Entrada  VALIDAR  PROCESO  Salida 

N , not  cap,  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 

c = 0 ;  c < N ;   c = c+1  for ( c=0 ;  c<N  ; c = c+1 ) 


{  do { System.out.print(“Ingrese  nota  : ”); 
Leer   not  not= Integer.parseInt(br.readLine()); 

not < 0   V  not > 20  while (not < 0  || not > 20); 
if( not >=11) 
v                    not ≥ 11                F  { cap = cap + 1; 

cap = cap + 1           cde=cde+1  else  { cde = cde + 1; 


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

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). 

Entrada  VALIDAR  PROCESO  Salida 

N , ed  cma,  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 

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); 
if( ed >=11) 
v  ed ≥ 18  F  { cma = cma + 1; 

cma = cma + 1    cme=cme+1  else  { cme = cme + 1; 


System.out.println(“Cantidad de mayores de edad=”+cma); 
Escribir : cma , cme  System.out.println(“Cantidad de menores de edad=”+cme); 
}  // llave fin de main 
Fin 
}   // llave  fin de la clase

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). 

Entrada  VALIDAR  PROCESO  Salida 

N , not  sap,  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 

c = 0 ;  c < N ;   c = c+1  for ( c=0 ;  c<N  ; c = c+1 ) 


{  do { System.out.print(“Ingrese  nota  : ”); 
Leer   not  not= Integer.parseInt(br.readLine()); 

not < 0   V  not > 20  while (not < 0  || not > 20); 
if( not >=11) 
v                    not ≥ 11                F  { sap = sap + not; 

sap = sap + not  sde=sde + not  else  { sde = sde + not; 


System.out.println(“Suma notas aprobados  = ”+ sap); 
Escribir : sap , sde  System.out.println(“Suma notas desaprobados= ”+ sde); 
}  // llave fin de main 
Fin 
}   // llave  fin de la clase

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). 

Entrada  VALIDAR  PROCESO  Salida 

N , sex  ch,  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) 

Entrada  VALIDAR  PROCESO  Salida 

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 

V  not< 11 Λ ed< 18  F  if( not < 11 &&  ed <18) 


{   dme++; 
dme = dme +1  } 
if(ed<18) 
V  ed< 18  F  {  snm = snm + not; 

snm =snm +not  } 
System.out.println(“Aprobados Mayores  = ”+ apm); 
Escribir : apm, dme, sn  System.out.println(“Desaprobados menores  = ”+ dme); 
Fin  System.out.println(“Suma notas de menores  = ”+ snm); 
}  // llave fin de main 
}   // llave  fin de la clase

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). 

Entrada  VALIDAR  PROCESO  Salida 

N , Num  cp,  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 

S-ar putea să vă placă și