Lab Journal 05 Enroll 03 135241 058
Lab Journal 05 Enroll 03 135241 058
Objective(s):
Upon completion of this lab session, students will be able to:
• Understanding of Nested If statement
• Understanding of Switch statement
• Understanding of Loop statements
Tasks
Task 1
Serendipity Booksellers has a book club that awards points to its customers based on the
number of books purchased each month. (Use Nested if)
The points are awarded as follows:
• If a customer purchases 0 books, he or she earns 0 points.
• If a customer purchases 1 book, he or she earns 5 points.
• If a customer purchases 2 books, he or she earns 15 points.
• If a customer purchases 3 books, he or she earns 30 points.
• If a customer purchases 4 or more books, he or she earns 60 points.
Write a program that asks the user to enter the number of books that he or she has purchased
this month and then displays the number of points awarded.
TASK-1
#include <iostream>
using namespace std;
int main() {
int numBooks, points;
cout << "Enter the number of books purchased this month: ";
cin >> numBooks;
%
if (numBooks == 0) {
points = 0;
}
else if (numBooks == 1) {
points = 5;
}
else if (numBooks == 2) {
points = 15;
}
else {
if (numBooks == 3) {
points = 30;
}
else {
if (numBooks >= 4) {
points = 60;
}
}
}
cout << "Points awarded: " << points << endl;
return 0;
}
Task 2
Write a C++ Program for a scientific calculator using a switch statement.
#include<iostream>
#include<cmath>
int main() {
// Declearing Variables
char op;
double number1, number2, result, input;
cout << "Enter the operator you want to perform (+, -, *, /, ^, s, c, t, l, e, r, a, p, f, h):";
cin >> op;
switch (op)
{
case '+':
result = number1 + number2; //Addition
break;
case '-':
result = number1 - number2; //Subtraction
break;
case '*':
result = number1 * number2; //Multiplication
break;
case '/':
result = number1 / number2; //Division
break;
Page 2 of 9
%
case '^':
result = pow(number1, number2); //Exponentiation
break;
case's':
result = sin(number1); //Sin 0
break;
case 'c':
result = cos(number1); //Cos 0
break;
case 't':
result = tan(number1); //Tan 0
break;
case 'l':
result = log(number1); //Logarithm
break;
case 'e':
result = exp(number1); //Exponential
break;
case 'r':
result = sqrt(number1); //Square Root
break;
case 'a':
result = fabs(number1); //Absolute Value
break;
case 'p':
result = pow(number1, 1.0 / number2); //Power
break;
case 'f':
result = floor(number1); //Floor
break;
case 'h':
result = hypot(number1, number2); //Hypotenuse
break;
default:
cout << " Invalid Operator";
result - 1;
}
system("pause");
return 0;
}
Task 3
Page 3 of 9
%
Task#3
#include <iostream>
using namespace std;
int main() {
int test;
float temperature;
if (temperature <= 0)
test = 1;
else if (temperature <= 20)
test = 2;
else if (temperature <= 30)
test = 3;
else
test = 4;
switch (test) {
case 1:
cout << "Freezing" << endl;
break;
case 2:
cout << "Cold" << endl;
break;
case 3:
cout << "Moderate" << endl;
break;
case 4:
cout << "Hot" << endl;
break;
default:
cout << "Invalid temperature" << endl;
break;
}
return 0;
}
Output:
Page 4 of 9
%
Task 4
Write a program that uses a loop to display the characters for the ASCII codes 0 to 127.
Display 16 characters on each line.
TASK-4
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i <= 127; ++i) {
cout << char(i) << " ";
if ((i + 1) % 16 == 0)
cout << endl;
}
return 0;
}
Task 5
By applying for loop, write a C++ program that prints
a) A sequence of numbers in ascending order from 1 to 100 (inclusive).
b) Modify your program in part (a) to make it prints odd numbers within 1 to 100.
c) Write a C++ Program that receives a positive integer (N) and prints a series of
numbers from 1 to N (inclusive) in ascending order.
d) Write a C++ Program that displays a series of alphabets in descending order from ‘Z’
to ‘A’.
e) Modify your program in part (d) so that the program will display consonants only, no
vowels.
f) Write a C++ program that receives start value and end value. Then, your program will
display a series of numbers from the start value to the end value inclusive.
TASK-5
Page 5 of 9
%
(a)
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 100; ++i) {
cout << i << " ";
}
return 0;
}
Output:
(b)
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 100; i += 2) {
cout << i << " ";
}
return 0;
}
(c)
#include <iostream>
using namespace std;
int main() {
int N;
cout << "Enter a positive integer (N): ";
cin >> N;
(d)
#include <iostream>
using namespace std;
int main() {
char letter = 'Z';
while (letter >= 'A') {
cout << letter << " ";
--letter;
}
return 0;
}
Output:
(e)
#include <iostream>
using namespace std;
int main() {
char letter = 'Z';
while (letter >= 'A') {
if (letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U') {
cout << letter << " ";
}
--letter;
}
return 0;
}
Output:
(f)
#include <iostream>
using namespace std;
Page 7 of 9
%
int main() {
int start, end;
cout << "Enter the start value: ";
cin >> start;
cout << "Enter the end value: ";
cin >> end;
Task 6
Write a program by using a loop and display all Prime numbers. You cannot use nested or
more than one loop in this program.
Use only one loop
TASK-6
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Prime numbers between 2 and " << num << " are: " << endl;
if (isPrime)
cout << i << " ";
}
return 0;
}
Page 8 of 9
%
Max Obtained
Task Comments (if any)
Marks Marks
1. 1.66
2. 1.66
3. 1.67
4. 1.67
5. 1.67
6. 1.67
Total 10 Signature
Note: Attempt all tasks and get them checked by your Lab Instructor.
Page 9 of 9