Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Parcial 1

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 17

PRIMER PARCIAL

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

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;
if(a==b)
cout<<"Los dos numeros son iguales"<<endl; //Comparamos si nuestros numeros
son iguales
if(a>b)
cout<<a<<" Es mas grande que "<<b<<endl; //Comparamos si la primer variable
es la mas grande
if(a<b)
cout<<b<<" Es mas grande que "<<a<<endl; //Comparamos si la segunda
variable es mas grande
return 0;
}

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

suma es "<<suma<<endl;//impresiones de resultados


promedio es "<<promedio<<endl;
producto es "<<producto<<endl;
numero mas pequeo es "<<y<<endl;
numero mas grande es "<<x<<endl;

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

using namespace std;

int main()
{
cout<<"*********
<<"*

<<"*

***
*

***

*\n"//impresion de figuras

* *\n"

*****

* * \n"

<<"*

<<"*

<<"*

<<"*

* *\n"

<<"*

* *\n"

<<"*********

*
***

*
*

return 0;
}

2.22 What does the following code prints?


cout<< *\n**\n***\n****\n***** <<endl;
it does not have main, shows an error masage

*\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;

//Usando una sola instruccion cout

2.27(Integer Equivalent of a Character) Here is a peek ahead. In this chapter you


learned about integers and the type int. C++ can also represent uppercase letters,
lowercase letters and a considerable variety of special symbols. C++ uses small
internally to represent each different character. The set of character a computer uses
and the corresponding integer representations for those character are called that
computers character set. You can print a character by enclosing that character in
single quotes, as with
11

cout<< A; // print an uppercase A


You can print the integer equivalent of a character using static_case as follows:
cout << static_cast<int>( A ); //print A as an integer
This is called a cast operation (we formally introduce casts in Chapter 4). When the
preceding statement executes, it prints the value 65(on systems that use the ASCII
character set). Write a program that prints the integer equivalent of a character typed
at the keyboard. Store the input in a variable of type char. Test your program several
times using uppercase letters, lowercase letters; digits and special characters (likes $).
#include <iostream>//libreria
using namespace std;
int main(){
char ascii;//variable tipo caracter
int numeric;//variable tipo entero
cout << "Ecsribe un caracter: ";//pedir ingresar el valor de la
variable
cin >> ascii;//guardar el valor de l avariable
cout << "El valor ASCII es: " << (int) ascii << endl;//impresion
de resultado
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

if (numero >= 10000 || numero <= 99999)


el numero dado por el usuario sea de 5 digitos
{

//Para asegurarnos de que

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

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

BMI = weight In Pounds * 703/height In Inches * height In Inches


Or
BMI = height In Kilograms / height In Meters * height In Meters
Create a BMI calculator application that reads the users weight in pounds and height
in inches (or, if you prefer, the users weight in kilograms and height in meters), then
calculates and displays the users body mass index. Also, the application should
display the following information from the department of Health and Human
Services/ National Institutes of Health so the user can evaluate his/her BMI
2.31 (Car-pool savings calculator) Research several car-pooling websites. Create an
application that calculates your daily driving cost, so that you can estimate how
money could be saved by carpooling, which also has other advantages such as
reducing carbon emissions and reducing traffic congestion. The application should
input the following and display the users cost per day of driving to work:
a)

Total miles driven per day.

b)

Cost per gallon of gasoline.

c)

Average miles per gallon.

d)

Parking fees per day.

e)

Tolls per gay.

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

if( IMC <= 18.5 )


//Comparamos con los valores dados por la secretaria de salud para deternimar si el
usuario padece desnutricion
cout<<"Su IMC es BAJO\n Por lo tanto esta bajo de peso"<<endl;
//y si es
el caso mostramos el resultado de dicha comparacion
if( IMC >= 18.5 && IMC <= 24.9 )
//Comparamos con los valores dados por la secretaria de salud para
deternimar si el usuario tiene un peso saludable
cout<<"Su IMC es NORMAL\n Por lo tanto usted tiene un peso saludable"<<endl;
//y si el caso mostramos el resultado
if( IMC >= 25 && IMC <= 29.9 )
//Comparamos con los valores dados por la secretaria de salud para deternimar
si el usuario padece alto peso
cout<<"Su IMC es ALTO\n Por lo tanto usted tiene sobre peso"<<endl;
// y
si es el caso mostramos el resultado
if( IMC >= 30 )
//Comparamos con los valores dados por la secretaria de salud para
deternimar si el usuario padece obesidad
cout<<"Su IMC es MUY ALTO\n Por lo tanto usted tiene obesidad"<<endl;
//y
si es el caso mostramos el resultado
return 0;
}
16

2.31(Car-pool savings calculator)Research several car-pooling websites. Create an


application that calculates your daily driving cost, so that you can estimate how
money could be saved by carpooling, which also has other advantages such as
reducing carbon emissions and reducing traffic congestion. The application should
input the following and display the users cost per day of driving to work:
a)
b)
c)
d)

Total miles driven per day.


Cost per gallon of gasoline.
Average miles per gallon.
Parking fees per day.
e) Tolls per gay.
#include <iostream>//librerias
#include <cmath>
#include <stdlib.h>
using namespace std;
int main ()
{
float millas_dia, costo_galon, millas_galon, park, caseta;//variables tipo
flotante
float rendimiento;
cout<<"\nIngrese las millas\n";//pedir ingresar el valor de la variable
cin>>millas_dia;//Guardar el valor de la variable
cout<<"\nIngrese el costo por galon\n";
cin>>costo_galon;
cout<<"\nIngrese el rendimiento de millas por galon\n";
cin>>millas_galon;
cout<<"\nIngrese lo gastado en casetas y otro\n";
cin>>park;
cout<<"\nIngrese lo gastado en casetas y otro\n";
cin>>caseta;
rendimiento=(millas_dia/millas_galon)/costo_galon;//operaciones
cout<<"\nLO QUE USTED GASTA
ES\n"<<rendimiento+caseta+park;//resultados
return 0;

17

También podría gustarte