You are on page 1of 10

PROGRAMAS FOR

1.
IMPAR O PAR FOR

#include <iostream>
#include <string>

using namespace std;


int main()
{
int a;
int b;
int c;

cout<<"Par o Impar\n";
cout<<"Ingrese un numero\n";
cin>>a;

cout<<"Ingrese un numero mayor o igual que el anterior\n";


cin>>b;

if (b<a)
{
cout<<"Lee de nuevo";
}
else
for (a;a<=b;a++)
{
c = (a%2);
if (c == 0)
cout<<"El numero "<<a <<" Es par\n" ;
else
cout<<"El numero "<<a <<" Es impar\n";
}

2.
#include <iostream>
#include <string>

using namespace std;


int main(int argc, char*argv[])
{
int num;
int i;
int div;
cout<<"divisores de un numero\n";
cout<<"Ingrese un numero \n";
cin>>num;
for(i=1;i<=num;i=i+1)
{
div= num%i;
if (div==0)
{
cout<<i<<" ";
}
}
cout<< endl;
}
3.
#include <iostream>
#include <string>

using namespace std;


int main(int argc, char*argv[])
{
cout << "NÚMEROS NEGATIVOS" << std::endl;
int valo;
cout << "¿Cuántos valores va a introducir? ";
cin >> valo;
if (valo < 0) {
cout << "¡Imposible!" << endl;
} else {
int contador = 0;
for (int i = 1; i <= valo; i++) {
float valor;
cout << "Escriba el número " << i << ": ";
cin >> valor;
if (valor < 0) {
contador = contador + 1;
}
}
if (contador == 0) {
cout << "No ha escrito ningún número negativo." << endl;
} else if (contador == 1) {
cout << "Ha escrito 1 número negativo." << endl;
} else {
cout << "Ha escrito " << contador << " números negativos." << endl;
}
}
return 0;
}
4.
#include <iostream>
using namespace std;
int main()
{
int a=2;
int b;
int c;

int x=3;
int y;
int z;

cout<<"Los multiplos de dos son: "<<endl;


for(b=1; b<=100;b++)
{
c=a*b;
if(c<=100)
cout<<c<<endl;
}

cout<<endl;

cout<<"Los multiplos de tres son: "<<endl;


for(y=1; y<=100;y++)
{
z=x*y;
if(z<=100)
cout<<z<<endl;
}
return 0;
}
5.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;

cout<<"Ingrese un numero: "<<endl;


cin>>a;

cout<<"La tabla del "<<a<<" es: "<<endl;


for(b=1; b<=15; b++)
{
c=a*b;
cout<<a<<"*"<<b<<"="<<c<<endl;
}
return 0;
}
EJERCICIOS WHILE
1.
#include <iostream>
using namespace std;
int main()
{
int a;
int b=1;

cout<<"Ingrese un numero: "<<endl;


cin>>a;

while(b<=a)
{
cout<<b<<endl;
b++;
}
return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
double c;
double d;

while(b<10)
{
cout<<"Ingrese un numero: "<<endl;
cin>>a;
c+=a;
b++;
}

//suma
cout<<"La suma de los numero es: "<<endl;
cout<<c<<endl;

//promedio
d=c/10;
cout<<"El promedio de los numero es: "<<endl;
cout<<d<<endl;

return 0;
}
3.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int d;

while(b<10)
{
cout<<"Ingrese su calificacion: "<<endl;
cin>>a;
b++;

if(a>=7)
c++;
else
d++;
}

cout<<endl;

cout<<"Los mayores a 7 fueron: "<<endl;


cout<<c<<endl;

cout<<endl;

cout<<"Los menores a 7 fueron: "<<endl;


cout<<d<<endl;

return 0;
}
4.
#include <iostream>
using namespace std;
int main()
{
int x;
int y=1;
int z;

cout<<"Ingrese un numero: "<<endl;


cin>>x;

cout<<"La tabla del numero ingresado es: "<<endl;


while(y<=12)
{
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl;
y++;
}
return 0;
}
5 pero en la guía dice 6
#include <iostream>
using namespace std;
int main()
{
int a=2;
int b=1;
int c;

int x=3;
int y=1;
int z;

cout<<"Los multiplos de 2 hasta 100 son: "<<endl;


while(c<100)
{
c=a*b;
cout<<c<<endl;
b++;
}

cout<<endl;
cout<<"Los multiplos de 3 hasta 100 son: "<<endl;
while(z<99)
{
z=x*y;
cout<<z<<endl;
y++;
}
return 0;
}

You might also like