1
1)What is the exact output of the program below? Indicate a blank space in the output
by writing
the symbol ! . Indicate a blank line in the output by writing blank line
A. Pseudocode
SET n = 4
SET k = 2
PRINT ++n
PRINT n
PRINT n++
PRINT n
PRINT -n
PRINT n
PRINT --n
PRINT n
PRINT n--
PRINT n
PRINT n + k
PRINT n
PRINT k
PRINT n, k
PRINT n
PRINT " ", n
PRINT " n"
PRINT "\n"
PRINT " n * n = "
PRINT n * n
PRINT 'n'
B. output
5
5
5
6
-6
6
5
5
5
4
6
4
2
4!2
4
!4
!n
blank line
!n * n = 16
n
2)What is the output of the program below?
A)Pseudocode
SET n = 3
WHILE n >= 0
PRINT n * n
DECREMENT n
PRINT n
2
WHILE n < 4
PRINT ++n
PRINT n
WHILE n >= 0
PRINT n /= 2
B) Output
9
4
1
0
-1
0
1
2
3
4
4
2
1
0
3) What is the output of the program below?
A) Pseudocode
DECLARE n
PRINT n = 4
PRINT n == 4
PRINT n > 3
PRINT n < 4
PRINT n = 0
PRINT n == 0
PRINT n > 0
PRINT n && 4
PRINT n || 4
PRINT !n
B) Output
4
1
1
0
0
1
0
0
1
1
4) What is the output of the following program?
A) Pseudocode
ENUM color_type {red, orange, yellow, green, blue, violet}
DECLARE shirt, pants
SET shirt = red
SET pants = blue
PRINT shirt, " ", pants
B) Output
05
3
5)What is the output when the following code fragment is executed?
A) Pseudocode
SET i = 5
SET j = 6
SET k = 7
SET n = 3
PRINT i + j * k - k % n
PRINT i / n
B) Output
46
1
6) Given the following operation, what will be the value of the variable q?
A) Pseudocode
SET j = 36
SET m = 6
SET q = j % m
B) Output
0
I)What is the output when the following code fragment is executed?
A) Pseudocode
PRINT "\n\n Print the result of some specific operation :\n"
PRINT "--------------------------------------------------\n"
PRINT " Result of 1st expression is : ", (-1 + 4 * 6)
PRINT " Result of 2nd expression is : ", ((35 + 5) % 7)
PRINT " Result of 3rd expression is : ", (14 + -4 * 6 / 11)
PRINT " Result of 4th expression is : ", (2 + 15 / 6 * 1 - 7 % 2)
B) Output
Print the result of some specific operation :
--------------------------------------------------
Result of 1st expression is : 23
Result of 2nd expression is : 5
Result of 3rd expression is : 12
Result of 4th expression is : 3
II) What is the output when the following code fragment is executed?
A)Pseudocode
PRINT "\n\n Formatting the output using type casting:\n"
PRINT "----------------------------------------------\n"
PRINT "Print floating-point number in fixed format with 1 decimal place: "
SET fixed format with 1 decimal place
PRINT "\nTest explicit type casting :\n"
SET i1 = 4
SET i2 = 8
PRINT i1 / i2
PRINT (double)i1 / i2
PRINT i1 / (double)i2
PRINT (double)(i1 / i2)
SET d1 = 5.5
SET d2 = 6.6
4
PRINT "\nTest implicit type casting :\n"
PRINT (int)d1 / i2
PRINT (int)(d1 / i2)
PRINT "\nint implicitly casts to double: \n"
SET d1 = i1
PRINT d1
PRINT "double truncates to int!: \n"
SET i2 = d2
PRINT i2
B)Output
Formatting the output using type casting:
----------------------------------------------
Print floating-point number in fixed format with 1 decimal place:
Test explicit type casting :
0
0.5
0.5
0.0
Test implicit type casting :
0
0
int implicitly casts to double:
4.0
double truncates to int!:
6
5
Question 7:
6
7
Write a C++ program to display the current date and time.
8
Write a program in C++ that converts kilometers per hour to miles per hour.
Write a program in C++ to convert temperature in Fahrenheit to Celsius.
9
12. Write a program in C++ to find the area and circumference of a circle.
PSEUDOCODE:
START
DISPLAY "Find the area and circumference of any circle"
DISPLAY "----------------------------------------------------"
PROMPT "Input the radius (1/2 of diameter) of a circle: "
READ radius
SET pi = 3.14159
COMPUTE area = pi * radius * radius
COMPUTE circumference = 2 * pi * radius
DISPLAY "The area of the circle is: ", area
DISPLAY "The circumference of the circle is: ", circumference
END
CODE:
#include <iostream>
#include <iomanip> // for precision
using namespace std;
int main() {
double radius, area, circumference;
const double pi = 3.14159;
cout << "Find the area and circumference of any circle :" << endl; FLOWCHART
cout << "----------------------------------------------------" << endl;
cout << "Input the radius (1/2 of diameter) of a circle : ";
cin >> radius;
area = pi * radius * radius;
circumference = 2 * pi * radius;
cout << fixed << setprecision(4);
cout << "The area of the circle is : " << area << endl;
cout << "The circumference of the circle is : " << circumference << endl;
return 0;
}
Sample output:
Find the area and circumference of any circle :
----------------------------------------------------
Input the radius (1/2 of diameter) of a circle : 5
The area of the circle is : 78.5397
The circumference of the circle is : 31.4159
10
13. Write a program in C++ to find the Area and Perimeter of a Rectangle.
Pseudocode:
START
DISPLAY "Find the Area and Perimeter of a Rectangle"
DISPLAY "-------------------------------------------------"
PROMPT "Input the length of the rectangle: "
READ length
PROMPT "Input the width of the rectangle: "
READ width
COMPUTE area = length * width
COMPUTE perimeter = 2 * (length + width)
DISPLAY "The area of the rectangle is: ", area
DISPLAY "The perimeter of the rectangle is: ", perimeter
END
Code:
#include <iostream>
using namespace std;
int main() {
double length, width, area, perimeter;
cout << "Find the Area and Perimeter of a Rectangle" << endl; Flow chart
cout << "-------------------------------------------------" << endl;
cout << "Input the length of the rectangle: ";
cin >> length;
cout << "Input the width of the rectangle: ";
cin >> width;
area = length * width;
perimeter = 2 * (length + width);
cout << "The area of the rectangle is: " << area << endl;
cout << "The perimeter of the rectangle is: " << perimeter << endl;
return 0;
}
Sample output:
Find the Area and Perimeter of a Rectangle
-------------------------------------------------
Input the length of the rectangle: 10
Input the width of the rectangle: 15
The area of the rectangle is: 150
The perimeter of the rectangle is:
11
14. write a program in C++ to swap two numbers using a temporary variable.
Pseudocode:
START
DISPLAY "Swap two numbers :"
DISPLAY "-----------------------"
PROMPT "Input 1st number: "
READ num1
PROMPT "Input 2nd number: "
READ num2
SET temp = num1
SET num1 = num2
SET num2 = temp
DISPLAY "After swapping the 1st number is: ", num1
DISPLAY "After swapping the 2nd number is: ", num2
END
Code:
#include <iostream>
using namespace std;
int main() {
int num1, num2, temp;
cout << "Swap two numbers :\n";
cout << "-----------------------\n";
cout << "Input 1st number : ";
cin >> num1;
cout << "Input 2nd number : ";
cin >> num2;
FLOWCHART
// Swapping logic using a temporary variable
temp = num1;
num1 = num2;
num2 = temp;
cout << "After swapping the 1st number is : " << num1 << endl;
cout << "After swapping the 2nd number is : " << num2 << endl;
return 0;
}
12
15. Write a program in C++ to print the sum of two numbers using variables.
Pseudocode:
START
DISPLAY "Print the sum of two numbers:"
DISPLAY "23"
DISPLAY "C++ Programming Language"
DISPLAY "Lecture Five - Expressions & Operators in C++"
DISPLAY "-----------------------------------"
SET a = 29
SET b = 30
COMPUTE sum = a + b
DISPLAY "The sum of 29 and 30 is: ", sum Flowchart:
END
Code:
#include <iostream>
using namespace std;
int main() {
int a = 29, b = 30, sum;
cout << "Print the sum of two numbers:" << endl;
cout << "23" << endl;
cout << "C++ Programming Language" << endl; Flow chart
cout << "Lecture Five - Expressions & Operators in C++" << endl;
cout << "-----------------------------------" << endl;
sum = a + b;
cout << "The sum of 29 and 30 is: " << sum << endl;
return 0;
}
13
16. To what do the following expressions evaluate?
a. 17/3
b. 17%3
c. 1/2
d. 1/2*(x+y)
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 5; // You can change x and y to any values
cout << "a. 17 / 3 = " << 17 / 3 << endl; // 5 (integer division)
cout << "b. 17 % 3 = " << 17 % 3 << endl; // 2 (remainder)
cout << "c. 1 / 2 = " << 1 / 2 << endl; // 0 (integer division)
cout << "d. 1 / 2 * (x + y) = " << 1 / 2 * (x + y) << endl; // 0 (1/2 = 0, 0*(x+y) = 0)
return 0;
}
17.Given the declarations: float x; int k, i = 5, j = 2; To what would the variables x
and k be set as a result of the assignments
a. k = i/j;
b. b. x = i/j;
c. c. k = i%j;
d. d. x = 5.0/j;
#include <iostream>
using namespace std;
int main() {
float x;
int k, i = 5, j = 2;
k = i / j; // Integer division: 5 / 2 = 2
cout << "a. k = i / j = " << k << endl;
x = i / j; // Integer division happens first: 5 / 2 = 2 → x = 2.0
cout << "b. x = i / j = " << x << endl;
k = i % j; // Modulus: 5 % 2 = 1
cout << "c. k = i % j = " << k << endl;
x = 5.0 / j; // Floating-point division: 5.0 / 2 = 2.5
cout << "d. x = 5.0 / j = " << x << endl;
return 0;
}
14