problema c++

8
PRACTICA N°3 EJERCICIOS CON ESTRUCTURA REPETITIVA 1. Leer 10 números 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 leídos. #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;

Upload: alejandro-salazar

Post on 01-Dec-2014

8.952 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Problema c++

PRACTICA N°3

EJERCICIOS CON ESTRUCTURA REPETITIVA

1. Leer 10 números 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 leídos.

#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();}

Page 2: Problema c++

3. Calcular un número 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 números 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;

Page 3: Problema c++

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 número 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 número 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++;}

Page 4: Problema c++

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 números impares.

#include <cstdlib>

Page 5: Problema c++

#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 centímetros.

#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 números aleatorios mediantes la función rand() entre 1y 100.

#include <cstdlib>#include <iostream>#include <conio.h> #include <stdlib.h>using namespace std;

int main()

{ int a,b,c;

Page 6: Problema 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 variación al rand() for(c=1;c<=b;c++) cout<<rand()%100+1<<" "; getch();}

12. Ingresar un conjunto de números enteros finaliza si ingresa un número menor que 0 (cero). Calcular el promedio de los números 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 número es perfecto. Si la suma de sus divisores menores que el número es igual al mismo números.

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

Page 7: Problema c++

else cout<< "\nEL NUMERO NO ES PERFECTO" <<endl; getch();}