Lab (2)
Lab (2)
Introduction to Computing
Lab (2)
Exercise 1: write the output to each of the following programs
Problem (1): What is output of this program if the user responds to its enquiry by 5?
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Enter the number of students in the class : ";
cin >> n;
n = n + 3;
cout << "The number after adding three students : " << n;
return 0;
}
Solution:
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Problem (2): What is output of this program if the user responds to its enquiry by 4, then 3?
#include <iostream>
using namespace std;
int main(){
int n1, n2;
cout << "Enter the number of male students in the class : ";
cin >> n1;
cout << "Enter the number of female students in the class : ";
cin >> n2;
cout << "The total number of students is " << n1 + n2;
return 0;
}
Solution:
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
1
Problem (3)
#include <iostream>
Using namespace std;
int main()
{
int floors, rooms, suits = 30;
floors=15;
rooms=300;
cout<<”The grand hotel has”<<floors<<”floors\n”;
cout<< “with ”<<rooms<<” rooms and ”<<suits;
cout<<” suites.\n”;
return 0;
}
Solution
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Problem (4):
#include <iostream>
using namespace std;
int main()
{
number = 62;
int number;
cout << number <<endl;
return 0;
}
Solution:
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
2
Exercise 2: answer each of the following:
Problem (1): Choose all the variable definition statements that are acceptable in C++:
a) int a;
b) Int b;
c) int 4apples;
d) int apple4;
e) int G4;
f) int G@;
g) int a, b, c, d;
h) int a, b, c, d
i) int a b c d;
j) int GGGGGGGGGGGGGGGGGDFDDDDDDDDDDD;
k) int 111111111111111111111111;
l) int int;
m) int cout;
Problem (2): choose all the variable assignment statements that are acceptable in C++:
n) int a = 4;
o) a = 3;
p) 3 = a;
q) int a = = 3;
r) int a 3;
s) a 3
t) a = 3
Problem (3): Assume value is an integer variable. If the user enters 3.14 in response to the
following programming statement, what will be stored in value?
cin >> value;
u) 3.14
v) 3
w) 0
x) Nothing, an error will occur.
Problem (4): How would you write the following definitions into one statement and write one cin
statement that reads a value into each of these variables?
int x;
int y;
int z;
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
3
Exercise 3: use visual studio to write each of the following programs
Problem (1):
Write a program that asks the user to enter the length and width of a rectangle. Calculate the area
of the rectangle and display it to the user.
Input: 10, 20
Output:
This program calculates the area of a rectangle.
What is the length of the rectangle?
10 (Entered by the user)
What is the width of the rectangle?
20 (Entered by the user)
Area of rectangle = 200
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
Problem (2):
Write a program that asks the user to enter the numerator and denominator of a fraction and display
the value.
Input: 3, 16
Output:
This program shows the decimal value of a
fraction.
Enter the numerator and denominator.
3, 16 (Entered by the user)
The decimal value is = 0
4
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
Problem (3):
Complete the following code skeleton so it asks for the user to enter weight and display it in
kilograms.
#include <iostream>
using namespace std;
int main()
{
int pounds, kilograms;
// Write code here to ask the user to enter weight in pound
kilograms = pounds / 2.2;
// Write code here that displays the user's weight in kilograms.
return 0;
}
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
5
Problem (4):
Follow the following steps to write a program that calculates a customer’s available credit:
Instruction 1: Display the message “enter the customer’s maximum credit.”
Instruction 2: Wait for the user to enter the customer’s maximum credit.
Instruction 3: Display the message “enter the amount of credit used by the customer.”
Instruction 4: Wait for the user to enter the customer’s credit used.
Instruction 5: Subtract the used credit from the maximum credit to get the customer’s available
credit.
Instruction 6: Display a message that shows the customer’s available credit.
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
Problem (5):
Write a program that asks the user to enter two integer numbers to calculate their sum. Display the
sum value and its size.
Input: 5, 6
Output:
Enter two values to calculate their sum.
5 (Entered by the user)
6 (Entered by the user)
Sum = 11
Size of Sum = 4
Solution:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------