Está en la página 1de 6

PRACTICA N3

EJERCICIOS CON ESTRUCTURA REPETITIVA



1. Leer 10 nmeros enteros y calcular la suma
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[])
{ int a,i=1,j=0;
cout<<"INGRESE 10 NUMEROS"<<endl;
while(i<=10)
{cin>>a;
j=j+a;
i++;}
cout<<"LA SUMA ES"<<j<<endl;
getch(); }


2. Leer un conjunto de datos mientras que no sea el ltimo. Y calcular la suma de los datos ledos.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{ int a,b,m=1,s=0,j;
cout<<"ESPECIFIQUE LA CANTIDAD DE NUMEROS"<<endl;
cin>>a;
cout<<"INGRESE LOS "<<a<<" NUMEROS"<<endl;
while (m<=a)
{cin>>b;
s=s+b;
m++;}
cout<<"LA SUMA DE LOS "<<(a-1)<<" PRIMEROS NUMEROS ES "<<(s-b)
<<endl;
getch();}




3. Calcular un nmero a la potencia n.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a=1,b=1,n,m;
cout<< "NUMERO A LA N-ESIMA POTENCIA" <<endl;
cout<< "INGRESE *n*" <<endl;
cin>>n;
cout<< "INGRESE EL NUMERO" <<endl;
cin>>m;
while (a<=n)
{b=b*m;
a++;}
cout<<endl;
cout<< " " <<n<<endl;
cout<< "EL RESULTADO ES " <<m<< " = " <<b<<endl;
getch();}

4. Calcular la suma y promedio de un conjunto de nmeros comprendidos en un rango.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{ int a,b,c,s=0;
cout<< "ESPECIFIQUE EL RANGO: [a,b]" <<endl;
cout<< "a=";
cin>>a;
cout<< "b=";
cin>>b;
c=b-a+1;
while (a<=b)
{s=s+a;
a++;}
cout<< "LA SUMA DEL CONJUNTO ES " <<s<<endl;
cout<< "EL PROMEDIO DEL CONJUNTO ES " <<s/c<<endl; getch();}

5. Mostrar valores mayores que 50 y que asciendan en 2 unidades supongamos que el nmero tope es
100.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a,i;
cout<< "INGRESE UN NUMERO MENOR QUE 100" <<endl;
cin>>a;
cout<< "LOS VALORES QUE ASCIENDEN DE 2 EN 2 MAYORES DE 50 A HASTA
100 SON " <<endl;
while (a<i<99-a)
{a=a+2;
if(a>50)
cout<<a<< " " ;
else
i++;}
getch();}

6. Dado un numero disminuir el nmero de 2 en 2 para valores mayores e iguales que 50.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a,b,i=1;
cout<< "DE UN VALOR MAYOR O IGUAL A 50" <<endl;
cin>>a;
cout<< "LOS VALORES SON" <<endl;
while (i<a)
{a=a-2;
if(a>=0)
cout<<a<< " " ;
else
i++;}
getch();}



7. Calculo del factorial de un numero usar while.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a,b=1,x=1;
cout<< "FACTORIAL DE UN NUMERO\n" <<endl;
cout<< " INGRESE EL NUMERO" <<endl;
cin>>a;
while(x<=a)
{b=b*x;
x++;}
cout<< "\nEL RESULTADO ES " <<a<< "!" << " = " <<b<<endl;
getch();}

8. Hallar la inversa de un numero entero.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a,b=0,c=0;
cout<< "DE UN NUMERO ENTERO\n" <<endl;
cin>>a;
while (a>0)
{b=a%10;
c=c*10+b;
a=a/10;}
cout<< "\nEL NUMERO INVERTIDO ES => " <<c<<endl;
getch();}

9. Calcular el cuadrado de un numero entero es hallar mediante la suma de los n primeros nmeros
impares.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int a,b,n=0;
cout<< "CALCULO DEL CUADRADO DE UN NUMERO POR SUMA DE NUMEROS
IMPARES\n" <<endl;
cout<< "INGRESE EL NUMERO\n" <<endl;
cin>>a;
for(b=1;b<=a;b++)
n=n+2*b-1;
cout<< "\nEL CUADRADO DE " <<a<< " ES " <<n;
getch();}

10. Convertir una longitud dada en pies a metros y centmetros.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{float m,c,p;
cout<< "INGRESE LA MEDIDA EN PIES\n" <<endl;
cin>>p;
c=p/30.48;
m=100*c;
cout<< "\nLA MEDIDA EN METROS ES " <<m<<endl;
cout<< "\nLA MEDIDA EN CENTIMETROS ES " <<c<<endl;
getch();}

11. Generar un conjunto de nmeros aleatorios mediantes la funcin rand() entre 1y 100.
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main()

{ int a,b,c;
cout<< "DE LA CANTIDAD DE DATOS\n" <<endl;
cin>>b;
cout<< "\nLOS NUMEROS ELEGIDOS AL AZAR ENTRE 1 Y 100 SON:\n\n" ;
srand(time(0)); // <-- para darle variacin al rand()
for(c=1;c<=b;c++)
cout<<rand()%100+1<<" ";
getch();}

12. Ingresar un conjunto de nmeros enteros finaliza si ingresa un nmero menor que 0 (cero). Calcular el
promedio de los nmeros ingresados.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{float p,a,b=0,s=0;
cout<< "INGRESE LOS NUMEROS ENTEROS CUALESQUIERA" <<endl;
while(a>=0)
{cin>>a;s=s+a;b++;}
p=(s-a)/(b-1);
cout<< "EL PROMEDIO DE LOS NUMEROS POSITIVOS ES " <<p<<endl;
getch();}

13. Probar si un nmero es perfecto. Si la suma de sus divisores menores que el nmero es igual al mismo
nmeros.
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()

{int s=0,x,n,m=0;
cout<< "NUMERO PERFECTO\n" <<endl;
cout<< "INGRESE UN NUMERO ENTERO\n" <<endl;
cin>>x;
for(n=1;n<x;n++)
{ if(x%n==0)
s=s+n; }
if(s==x)
cout<< "\nEL NUMERO ES PERFECTO" <<endl;
else
cout<< "\nEL NUMERO NO ES PERFECTO" <<endl;
getch();}

También podría gustarte