Parcial 1
Parcial 1
Parcial 1
UNIVERSIDAD DE GUANAJUATO
DICIS Dem Yuriria
Programacin en Ingeniera
9/28/2015
M.I.Ana Laura Martnez Herrera
Edgar Lpez Garca
Claudia Olidia Fuentes Ortiz
2.16(Arithmetic) Write a program that asks the user to enter two number obtains the
two numbers from the user and prints the sum, product, difference, and quotient of
the two numbers.
#include <iostream>
#include <cmath> //Librerias
using namespace std;
int main ()
{
float a, b; //Las variables a considerar
cout<<"Su primer numero?"<<endl; //Valor dado por el usuario y su respectiva
asignacion a nuestra variable
cin>>a;
cout<<"Su segundo numero?"<<endl; //Valor dado por el usuario y su
respectiva asignacion a nuestra variable 2
cin>>b;
cout<<"La Suma es: "<< a + b <<endl;
// Suma
cout<<"El Producto es: "<< a * b <<endl; //Producto
cout<<"La Resta es: "<< a - b <<endl;
//Resta
cout<<"El Cociente es: "<< a / b <<endl; // Division
return 0;
}
2.17 (Printing) write a program that prints the numbers 1 to 4 on the same line with
each pair of adjacent speared by one space. Do this serval ways:
a) Using one statement whit one space. Do this serval ways:
b) Using one statement with one stream insertion operator.
C) Using for statements
#include <iostream> //librerias
using namespace std;
int main()
{
int a,b,c,d; //variables
a=1; //valor de cada variable
b=2;
c=3;
d=4;
cout<<"1, 2, 3, 4.\n"; //impresiones
cout<<a<<","<<b<<","<<c<<","<<d<<".\n";
cout<<a<<",";
cout<<b<<",";
cout<<c<<",";
cout<<d<<".";
return 0;
}
2.18 (Comparing Integers) Write a program that asks the user to enter two integers
obtains the numbers from the user, then prints the larger number followed by word
is larger. If the numbers are equal, print the message These number are equal.
#include <iostream>
#include <cmath> //Librerias
using namespace std;
int main ()
{
int a, b;
//Variables a considerar
2.19(Arithmetic, Smallest and Largest) Write a program that inputs three integers
from the keyboard and prints the sum, average, product, smallest and largest of these
numbers. The screen dialog should appear as follows:
#include <iostream> //libreria
using namespace std;
int main()
{
float a,b,c,suma,producto,promedio, x, y; //variables tipo flotantes (esto
fue por el promeio)
cout<<"Introduzca tres numeros\n";//pedir ingresar el valor de la variables
cin>>a>>b>>c;//guardar el valor de las variables
suma=a+b+c;
promedio=(a+b+c)/3;//operaciones
producto=a*b*c;
x=a;//mayor que
if(b>x)
x=b;
if(c>x)
x=c;
y=a;//menor que
if(b<y)
y=b;
if(c<y)
y=c;
cout<<"\n
cout<<"\n
cout<<"\n
cout<<"\n
cout<<"\n
la
el
el
el
el
return 0;
}
5
2.20 (Diameter, Circumference and Area of a Circle) Write a program that reads in
the reads in the radius of a circle as an integer and prints the circles diameter,
circumference and area. Use the constant value 3.14159 for . Do all calculations in
output statements [Note: In this chapter, weve discussed only integer constants and
variables. In Chapter 4 we discuss floating-point numbers, i.e., values that can have
decimal points.]
#include <iostream>
#include <cmath> //Librerias
using namespace std;
int main ()
{
int radio;
cout<<"Introduzca el radio del circulo"<<endl; //Pedimos el radio del circulo
al usuario y asignamos el valor a la variable radio
cin>>radio;
cout<<"El Diametro es: "<< radio * 2 <<endl; //Calculamos el diametro 2R
cout<<"La Circunferencia es: "<< float(2 * (radio * 3.1416))<<endl;
//Calculamos el perimetro 2piR
cout<<"El Area es: "<<float(3.1416 * (pow(radio,2)))<<endl; //Calculamos el
area del circulo piR^2
return 0;
}
2.21 (Displaying Shapes with Asterisks) Write a program that prints a box, an oval, an
arrow and a diamond as follows:
#include <iostream>//librerias
int main()
{
cout<<"*********
<<"*
<<"*
***
*
***
*\n"//impresion de figuras
* *\n"
*****
* * \n"
<<"*
<<"*
<<"*
<<"*
* *\n"
<<"*
* *\n"
<<"*********
*
***
*
*
return 0;
}
*\n"
*\n"
*\n"
*\n";
2.23 (Largest and Smallest Integers) Write a program that reads in five tegers and
determines and prints the largest and the smallest integers in the group. Use only the
programming techniques you learned in this chapter
#include<iostream>//librerias
using namespace std;
int main()
{
int a,b,c,d,e,x,y;//variables tipo enteros
cout<<"introduzca 5 numeros enteros\n";//pedir ingresar el valor de
la variables
cin>>a>>b>>c>>d>>e;//valor de las variables
x=a;//mayor que
if(b>x)
x=b;
if(c>x)
x=c;
if(d>x)
x=d;
if(e>x)
x=e;
y=a;//menor que
if(b<y)
y=b;
if(c<y)
y=c;
if(d<y)
y=d;
if(e<y)
y=e;
cout<<"el numero mas grande es \n"<<x<<endl;//impresion de
resultados
cout<<"el numero mas pequeo\n"<<y<<endl;
return 0;
}
2.24(Odd or Even) Write a program that reads an integer and determines and prints
whether its odd or even. [Hint: Use modulus operator. An even number is a multiple
of two. Any multiple of two. Any multiple of two leaves a reminder of zero when
divided by 2.]
#include <iostream>
#include <cmath> //Librerias
using namespace std;
int main ()
{
int a;
cout<<"Su numero?"<<endl; //Pedimos el numero y lo asignamos a la
variable a
cin>>a;
if(a%2==0)
//Calculamos el modulo es decir si el resultado de la
operacion es 0 la variable resultante es un numero cerrado sin punto decimal
cout<<"Es Par"<<endl; //Como el resultado es cero la variable se
puede dividir entre 2 lo cual indica que es un numero par
else
cout<<"ES Impar"<<endl; //Como el resultado no es cero significa
que la division no es exacta por lo tanto el numero es impar
return 0;
}
2.25 (Multiples) Write a program that reads in two integers and determines and
determines and prints if the first is a multiple of the second. [Hint: Use the modulus
operator]
9
#include<iostream>//libreria
using namespace std;
int main()
{
int a,b;//variables tipo entero
cout<<"Ingrese dos numeros enteros\n";//pedir ingresar el valor de la
variables
cin>>a>>b;// Guardar valor de las variables
if(a%b==0)//condicion
cout<<a<<" es multiplo de "<<b<<endl;//resultado
return 0;
}
2.26 (checkerboard pattern) Display the following checkerboard pattern with eight
output statements, then display the same pattern using as few statements as possible.
10
#include <iostream>
#include <cmath>
//Librerias
using namespace std;
int main ()
{
//PRIMERA PARTE
cout<<"* * * * * * * *"<<endl;
cout<<" * * * * * * * *"<<endl;
cout<<"* * * * * * * *"<<endl;
cout<<" * * * * * * * *"<<endl;
//Usando una instruccion por fila se
puede seccionar la impresion en pantalla
cout<<"* * * * * * * *"<<endl;
cout<<" * * * * * * * *"<<endl;
cout<<"* * * * * * * *"<<endl;
cout<<" * * * * * * * *\n\n\n"<<endl;
//SEGUNDA PARTE
cout<<"* * * * * * * *"<<endl
<<" * * * * * * * *"<<endl
<<"* * * * * * * *"<<endl
<<" * * * * * * * *"<<endl
se puede imprimir practicamente todo
<<"* * * * * * * *"<<endl
<<" * * * * * * * *"<<endl
<<"* * * * * * * *"<<endl
<<" * * * * * * * *"<<endl;
return 0;
2.28 (Digits of an Integer) Write a program that inputs a five-drit integer, separate the
integer into is digits and prints them separated by three space each. [Hint: Use the
integer division and modulus operators.]
12
#include <iostream>
#include <cmath>
//Librerias
using namespace std;
int main()
{
int numero, primero, segundo, tercero, cuarto, quinto;
cout<<"Numero entero (5 digitos)"<<endl;
numero de 5 digitos y lo guardamos en la variable numero
cin>>numero;
//Pedimos al usuario un
2.29 (Table) Using the techniques of t this chapter , write a program that calculates the
squares and cubes of the integers from 0 to 10. Use tabs to print the following neatly
formatted table of values:
13
#include <iostream>
#include <cmath>
//Librerias
using namespace std;
Making a
int main()
{
int numero, primero, segundo, tercero, cuarto, quinto;
cout<<"Numero entero (5 digitos)"<<endl;
//Pedimos al
usuario un numero de 5 digitos y lo guardamos en la variable numero
cin>>numero;
if (numero >= 10000 || numero <= 99999)
//Para
asegurarnos de que el numero dado por el usuario sea de 5 digitos
{
primero = numero / 10000;
//Dividimos el numero entre 10000 para obtener el primer digito esto se
garantiza gracias a que nuestro valor es del tipo entero
segundo = (numero % 10000) / 1000;
//Calculamos el modulo del numero y este resultado lo dividimos entre
1000 para obtener el segundo digito
tercero = (numero % 10000) % 1000 / 100;
//Calculamos el modulo del numero y a este resultado le volvemos a sacar
el modulo posteriormente se divide entre 100 para obtener el tercer digito
cuarto = ((numero % 10000) % 1000) % 100 / 10;
//Calculamos el modulo del numero y a este resultado le volvemos a sacar el
modulo y una vez mas posteriormente se divide entre 10 para obtener el cuarto
digito
quinto = (((numero % 10000) % 1000) % 100) % 10;
//Calculamos el modulo cuatro veces y el resultado lo dividimos (o no) entre 1
para asi obtener el ultimo digito
cout<<primero<<" "<<segundo<<" "<<tercero<<"
"<<cuarto<<" "<<quinto; //Hacemos la impresion en pantalla de los digitos
con 3 espacios entre cada uno de ellos
}
return 0;
}
Difference
14
2.30(Body Mass index Calculator) we introduced the body mass index (BMI)
calculator in exercise 1.9 the formulas calculating BMI are
b)
c)
d)
e)
15
#include <iostream>
#include <cmath> //Librerias
using namespace std;
int main ()
{
double kilos, altura, IMC;
cout<<"Digame su peso (en kilogramos p.ej: 60.400)"<<endl;
su peso y lo guardamos en la variable kilos
cin>>kilos;
cout<<"Digame su altura (en metros p.ej: 1.34)"<<endl;
su estatura y lo guardamos en la variable alltura
cin>>altura;
//Pedimos al usuario
//Pedimos al usuario
IMC = kilos/(pow(altura,2));
//Calculamos IMC con los datos antes dados
cout<<"Su Indice de Masa Corporal (IMC) es: "<<IMC<<endl;
IMC al usuario
//Damos el valor de
17