0% found this document useful (0 votes)
14 views9 pages

Lab Journal 05 Enroll 03 135241 058

The document is a lab journal for a Computer Programming course at Bahria University, detailing various programming tasks using C++. It includes objectives related to understanding nested if statements, switch statements, and loop statements, along with specific programming assignments such as calculating points for book purchases, creating a scientific calculator, and displaying ASCII characters. The document also contains a grading sheet for the tasks completed.

Uploaded by

ibrahimkhann1610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Lab Journal 05 Enroll 03 135241 058

The document is a lab journal for a Computer Programming course at Bahria University, detailing various programming tasks using C++. It includes objectives related to understanding nested if statements, switch statements, and loop statements, along with specific programming assignments such as calculating points for book purchases, creating a scientific calculator, and displaying ASCII characters. The document also contains a grading sheet for the tasks completed.

Uploaded by

ibrahimkhann1610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 05

Course : Computer Programming - Lab Date: _______________


Course Code: CSL-113 Max Marks: 10
Faculty’s Name:

Name: _____________________ Enroll No: ___________________ Class: _____________

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

Enrollment Number: 03-135241-058

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>

using namespace std;

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;

cout << "Enter the numbers: ";


cin >> number1 >> number2;

// To perform the user desire operation we used switch statement

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
%

Enrollment Number: 03-135241-058

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

cout << " Result: " << result << endl;

system("pause");
return 0;
}

Task 3
Page 3 of 9
%

Enrollment Number: 03-135241-058

Write a C++ Program for Temperature Evaluation using switch statements.


Temperature<=0  Freezing
0<Temperature<=20  Cold
20<Temperature<=30  Moderate
Temperature>30  Hot

Task#3
#include <iostream>
using namespace std;

int main() {
int test;
float temperature;

cout << "Enter the temperature: ";


cin >> 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
%

Enrollment Number: 03-135241-058

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
%

Enrollment Number: 03-135241-058

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

for (int i = 1; i <= N; ++i) {


cout << i << " ";
}
return 0;
}
Page 6 of 9
%

Enrollment Number: 03-135241-058

(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
%

Enrollment Number: 03-135241-058

int main() {
int start, end;
cout << "Enter the start value: ";
cin >> start;
cout << "Enter the end value: ";
cin >> end;

for (int i = start; i <= end; ++i) {


cout << i << " ";
}
return 0;
}
Output:

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 << "Enter the maximum number: ";


cin >> num;

cout << "Prime numbers between 2 and " << num << " are: " << endl;

for (int i = 2; i <= num; ++i) {


int isPrime = 1;

for (int j = 2; j * j <= i; ++j) {


if (i % j == 0) {
isPrime = 0;
break;
}
}

if (isPrime)
cout << i << " ";
}

return 0;
}

Page 8 of 9
%

Enrollment Number: 03-135241-058

Lab Grading Sheet:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy