Lab Manual 1
Lab Manual 1
Programming Fundamentals
Submitted to:
Ma’am Ms. Neha Zulfiqar
Submitted by:
MUHAMMAD RABEE QURESHI
(SU92-BSCSM-F23-068)
PROGRAM:
BSCS 1ST SEMESTER
SECTION-1C
SESSION:
2023-27
SUPERIOR
UNIVERSITY GOLD CAMPUS LAHORE
1
Week: 01
Task-1
Write Hello World and I am a Programmer.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
cout << "I am a Programmer\n";
}
Output:
Task-2
2
Draw. *****
****
Code: ***
#include <iostream> **
using namespace std;
*
int main()
{
cout << "*****\n";
cout << " ****\n";
cout << " ***\n";
cout << " **\n";
cout << " *\n";
}
Output:
*
Task-3 **
***
Draw
****
Code: *****
3
#include <iostream>
using namespace std;
int main()
{
cout << "*\n";
cout << "**\n";
cout << "***\n";
cout << "****\n";
cout << "*****\n";
}
Output:
Task-4 *
***
*****
Draw *******
Code: *****
***
#include <iostream> *
using namespace std;
4
int main()
{
cout << " * \n";
cout << " *** \n";
cout << " ***** \n";
cout << " ******* \n";
cout << " ***** \n";
cout << " *** \n";
cout << " * \n";
} Output:
Task-5 *********
* *
Draw * *
* *
Code: * *
#include <iostream> * *
using namespace std; *********
int main()
5
{
cout << "*********\n";
cout << "*\t*\n";
cout << "*\t*\n";
cout << "*\t*\n";
cout << "*\t*\n";
cout << "*\t*\n";
cout << "*********\n";
}
Output:
Task-6
Draw
*
Code: **
***
#include <iostream> ****
using namespace std; *****
int main()
{
6
cout << " *\n **\n ***\n ****\n*****\n";
}
Output:
Task-7
Draw
*****
Code: ****
***
#include <iostream> **
using namespace std; *
int main()
{
cout << "*****\n ****\n ***\n **\n *\n";
}
Output:
7
Week: 02
Task-1
Write a program to get two values from the
user and apply logical operations on them.
Code:
#include <iostream>
using namespace std;
int main()
{
int a, b, sum, sub, multi, division;
cout << "Enter two numbers \n";
cin >> a >> b;
sum = a + b;
sub = a - b;
multi = a * b;
division = a % b;
8
cout << "Sum is = " << sum << endl << "Subtraction is =
" << sub << endl << "Multiplication is = " << multi <<
endl << "Division is = " << division << endl ;
}
Output:
Task-2
Write a program to swap two numbers with
supporting variables.
Code:
#include<iostream>
using namespace std;
int main()
{
int a, b, swap;
9
cout << "Enter two numbers" << endl;
cin >> a >> b;
swap = a;
a = b;
b = swap;
cout << endl << "swapping ... " << endl << endl << a << " " <<
b << endl << endl;
}
Output:
Task-3
Write a program to swap two numbers without
supporting variables.
Code:
#include <iostream>
using namespace std;
int main()
10
{
int a, b;
cout << "Enter two numbers"<<endl;
cin >> a >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "The answer is …. " << endl << a << " " << b;
}
Output:
Task-4
Write a program that take two inputs and
find quotient and remainder.
Code:
#include <iostream>
using namespace std;
int main()
{
float dividend, divisor, quotient, remainder;
11
cout << "Divide - ";
cin >> dividend;
cout << endl << "by - ";
cin >> divisor;
Output:
Task-5
Write a program that find the size of data
types which include int (short int, signed int,
and unsigned int), float (long and double),
char, string, and Boolean.
12
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Size of signed int is = " << "4 byte" <<endl;
cout << "Size of short int is = " << "2 byte" <<endl;
cout << "Size of unsigned int is = " << "4 byte" <<endl;
cout << "Size of float is = " << "4 byte" <<endl;
cout << "Size of double is = " << "8 byte" <<endl;
cout << "Size of char is = " << "1 byte" <<endl;
cout << "Size of string is = " << "24 byte" << endl;
cout << "Size of Boolean is = " << "1 byte" <<endl;
}
Output:
13
Week: 03
Task-1
If a =10, b = 12, c = 0. Find the values of each of
the following expressions. Write a
program to calculate and display the value of
each expression.
a. 1. a++ + ++a;
b. 2. b=b++ + b-- + a++ + ++a
c. 3. c=(c++ * b-- - ++a) / --c
d. 4. a=(c++ % ++b) + --a + a--
Code:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
float valueofc, valueofb, valueofa;
a = 10;
b = 12;
c = 0;
14
cout << "The Answer of [a++ + ++a] is = " << a++ + ++a <<
endl;
valueofb = b++;
valueofb = valueofb + b--;
valueofb = valueofb + a++;
valueofb = valueofb + ++a;
cout << "The Answer of [b++ + b-- + a++ + ++a] is = " <<
valueofb << endl;
}
Output:
15
Task-2
Write a program that will ask the user to input
a 5 digit number, calculate the sum of the
given 5 digits and display it. In case the user
enters an invalid number (less than or greater
than 5 digits)
the program should display a message ‘Sorry!
The number you
entered is NOT a 5 digit number’.
Hint: Use modulus operator to extract digits
from the number (5 digit number)
Code:
#include <iostream>
#include<string>
using namespace std;
int main() {
int z,sum , r;
cout << "Enter number = ";
cin>>z;
sum=0;
string zStr = to_string(z);
if (zStr.length() != 5) {
cout << "Sorry! The number you entered is NOT a 5 digit
number" << endl;return(0);}
while (z>0)
{
r=z%10;
17
sum = sum+r;
z=z/10;
}
cout<<"The sum of five numbers is "<<sum;
}
Output:
When the user enters 12345:
Task-3
A person invests $1000.0 in a savings account
yielding 5% interest.
Assuming that all interest is left on deposit in
the account,
18
Calculate and print the amount of money in
the account at the end
of each year for 10 years. Use the following
formula for
determining these amounts: a = p(1+r)n
were
p is the original
investment (i.e. the
principal) r is the annual
interest rate
n is the number of years
a is the amount on deposit at the end of the
nth year.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double p = 1000.00;
double rate = 0.05;
double n = 10;
19
cout << "Year" << "\t" << "Amount is" << endl <<
"___________________________" << endl << endl;
for (int y = 1; y <= 10; y++)
{
Week: 04
Task-1
20
Write a program that show the statement
“Admission successfully” if the marks
greater than 70, otherwise show “ineligible for
admission”.
Code:
#include<iostream>
}
Output:
21
Task-2
Write a program that take three inputs from
the user and find which one is highest
number.
Code:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers = ";
cin >> a >> b >> c;
if (a > b && a > c)
{
cout << "The largest number is = " << a;
}
else if (b > a && b > c)
{
cout << "The largest number is = " << b;
}
22
else
{
cout << "The largest number is = " << c;
}
Output:
Task-3
Write a program that take three inputs from
the user and find which one is smallest
number.
Code:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers = ";
cin >> a >> b >> c;
if (a < b && a < c)
{
cout << "The smallest number is = " << a;
}
else if (b < a && b < c)
23
{
cout << "The smallest number is = " << b;
}
else
{
cout << "The smallest number is = " << c;
}
}
Output:
Task-4
write a program that take input from the user
and show whether it is even or odd.
Code:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter a Number = ";
24
cin >> a;
if (a % 2 == 0)
{
cout << "It is an even number.";
}
else
{
cout << "It is an odd number.";
}
}
Output:
Task-5
Write a program that take two input value
from users, then as whether what operation
he/she perform in it.
a. Press 1 for applying SUM
b. Press 2 for applying MULTIPLY
c. Press 3 for applying DIVIDE
d. Press 4 for applying SUBTRACT
25
e. Take the input from these one of these 4
inputs and apply the specific
operation by using IF-ELSE-IF statement.
Code:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers = ";
cin >> a >> b;
int sum, multiply, subtract;
float divide;
sum = a + b;
subtract = a - b;
multiply = a * b;
divide = a / b;
int press_number;
cout << "Press 1 for applying SUM" << endl << "Press 2 for
applying MULTIPLY" << endl
<< "Press 3 for applying DIVIDE" << endl << "Press 4 for
applying SUBTRACT" << endl;
cin >> press_number;
26
if (press_number == 1)
{
cout << sum;
}
else if (press_number == 2)
{
cout << multiply;
}
else if (press_number == 3)
{
cout << divide;
}
else if (press_number == 4)
{
cout << subtract;
}
else
{
cout << "Press valid number plz.";
}
Output:
27
Task-6
Write a program that take the input as a year
from user and show whether it is leap
year or not.
Code:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter year = ";
cin >> a;
if (a % 4 == 0 && a % 100 == 0)
{
Output:
Task-7
Convert the flow of problem into a C++ code.
Code:
#include <iostream>
using namespace std;
int main()
{
int intermarks, enterytest_marks, interview_marks;
29
cout << endl << "-----------------------------------------" << endl
<< endl;
cout << "Enter your inter marks: ";
cin >> intermarks;
if (intermarks > 70)
{
cout << "You are Eligible for Entery Test." << endl <<
endl << "-----------------------------------------" << endl << endl;
cout << "Enter your entry test marks: ";
cin >> enterytest_marks;
if (enterytest_marks > 50)
{
cout << "You are Eligible for interview." << endl <<
endl << "-----------------------------------------" << endl << endl;
cout << "Enter your interview marks: ";
cin >> interview_marks;
cout << endl << "-----------------------------------------" <<
endl << endl;
if (interview_marks > 50)
{
int average = (intermarks + enterytest_marks +
interview_marks) / 3;
if (average > 50)
{
cout << "Admission successfully." << endl;
}
30
}
else
{
cout << "Apply Next year" << endl;
}
}
else
{
cout << "You are not Eligible good luck next time.";
}
}
else
{
cout << "You are not Eligible good luck next time.";
}
}
Output:
31
Week: 05
Task-1
Write a program that prompts the user to
input a number. The program should then
output the number and a message saying
whether the number is positive, negative, or
zero.
Code:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter a number = ";
cin >> a;
if (a >= 1)
{
cout << "The given number is positive.";
}
else if (a <= -1)
{
cout << "The given number is negative.";
32
}
else if (a == 0)
{
cout << "The given number is zero.";
}
}
Output:
Task-2
Write a program that mimics a calculator. The
program should take as input two
integers and the operation to be performed. It
should then output the numbers, the
operator, and the result. (For division, if the
denominator is zero, output an
appropriate message.) Some sample outputs
follow:
3+4=7
13 * 5 = 65
33
Code:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum, sub, divide, multi;
cout << "Enter first number = ";
cin >> num1;
char symbol;
cout << "Chose the desire symbol (+,-,*,/)" << endl;
cin >> symbol;
cout << "Enter secound number = ";
cin >> num2;
if (symbol == '+')
{
sum = num1 + num2;
cout << num1 << " + " << num2 << " = " << sum;
}
else if (symbol == '-')
{
sub = num1 - num2;
cout << num1 << " - " << num2 << " = " << sub;
}
else if (symbol == '*')
{
34
multi = num1 * num2;
cout << num1 << " * " << num2 << " = " << multi;
}
else if (symbol == '/')
{
divide = num1 / num2;
if (divide != 0)
{
cout << num1 << " / " << num2 << " = " << divide;
}
else if (divide == 0)
{
cout << "Error: Division by zero is not allowed." << endl;
return(0);
}
else
{
cout << "Select appropriate symbol.";
}
}
Output:
3 + 4 =7
35
13 * 5 =65
Task-3
One way to determine how healthy a person is
by measuring the body fat of the
person. The formulas to determine the body
fat for female and male are as follows:
Body fat formula for women:
A1= (body weight 0.732) + 8.987
A2= wrist measurement (at fullest point) /
3.140
A3= waist measurement (at navel) * 0.157
A4= hip measurement (at fullest point) * 0.249
36
A5= forearm measurement (at fullest point) *
0.434
B= A1 + A2 – A3 – A4 + A5
Body fat = body weight – B
Body fat percentage = body fat * 100 / body
weight
Body fat formula for men:
A1 = (body weight * 1.082) + 94.42
A2 = wrist measurement * 4.15
B = A1 – A2
Body fat = body weight – B
Body fat percentage = body fat * 100 / body
weight
Write a program to calculate the body fat of a
person.
Code:
#include <iostream>
#include<string>
using namespace std;
int main() {
char Gender;
37
cout << "Enter your gender, press m for male and f for female = ";
cin >> Gender;
if (Gender == 'M' || Gender == 'm')
{
float body_weight, measure_wrist;
double A1, A2, B;
cout << "Enter your body weight = ";
cin >> body_weight;
cout << "Enter your wrist measurement = ";
cin >> measure_wrist;
A1 = (body_weight * 1.082) + 94.42;
A2 = measure_wrist * 4.15;
B = A1 - A2;
float body_fat, bodyfat_percentage;
body_fat = body_weight - B;
bodyfat_percentage = body_fat * 100 / body_weight;
cout << "The fat in your body is = " << body_fat << endl << "And
body fat percentage is = " << bodyfat_percentage;
}
else if (Gender == 'F' || Gender == 'f')
{
float body_weight, measure_wrist;
double measure_waist, measure_hip, measure_forearm;
double A1, A2, A3, A4, A5, B;
cout << "Enter your body weight = ";
cin >> body_weight;
cout << "Enter your wrist measurement (at fullest point) = ";
cin >> measure_wrist;
38
cout << "Enter your waist measurement (at navel) = ";
cin >> measure_waist;
cout << "Enter your hip measurement (at fullest point) = ";
cin >> measure_hip;
cout << "Enter your forearm measurement (at fullest point) = ";
cin >> measure_forearm;
A2 = measure_wrist / 3.140;
A3 = measure_waist * 0.157;
A4 = measure_hip * 0.249;
A5 = measure_forearm * 0.434;
B = A1 + A2 - A3 - A4 + A5;
float body_fat, bodyfat_percentage;
body_fat = body_weight - B;
bodyfat_percentage = body_fat * 100 / body_weight;
cout << "The fat in your body is = " << body_fat << endl << "And
body fat percentage is = " << bodyfat_percentage;
}
else
{
39
cout << "Enter correct gender.";
}
Output:
Task-4
The statements in the following program are in
incorrect order. Rearrange the statements so
that they prompt the user to input the shape
type (rectangle, circle, or cylinder) and the
appropriate dimension of the shape. The
program then outputs the following
information about the shape: For a rectangle, it
outputs the area and perimeter; for a circle, it
outputs the area and circumference; and for a
40
cylinder, it outputs the volume and surface
area. After rearranging the statements, you
program should be properly indented.
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
int main() {
string shape;
double height, length, width, radius;
const double PI = 3.1416;
if (shape == "rectangle") {
cout << "Enter the length of the rectangle: ";
cin >> length;
41
cout << endl;
cout << "Area of the rectangle = " << length * width <<
endl;
cout << "Perimeter of the rectangle = " << 2 * (length +
width) << endl;
}
else if (shape == "circle") {
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << endl;
42
cout << "Enter the height of the cylinder: ";
cin >> height;
cout << endl;
return 0;
}
Output:
43
Week: 06
Task-1
Write a program that displays “Pakistan” five
times.
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++)
44
{
cout << i << ". Pakistan" << endl;
}
Output:
Task-2
Write a program that displays counting from 1
to 10.
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++)
{
cout << i << " ";
}
45
}
Output:
Task-3
Write a program that displays the first five
numbers with their square.
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++)
{
cout << i << " its square = " << i * i << endl;
}
Output:
46
Task-4
Write a program that displays back counting
from 10 to 1.
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 10; i >= 1; i--)
{
cout << i << " ";
}
Output:
Task-5
Write a program that displays the product of
all odd numbers from 1 to 10.
Code:
47
#include <iostream>
using namespace std;
int main() {
int product = 1;
for (int i = 1; i <= 10; i += 2)
{
product *= i;
}
cout << "The product is = " << product;
}
Output:
Task-6
Write a program that print following pattern.
Code:
#include <iostream>
using namespace std;
int main() {
//1st pattren
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
48
{
cout << "*";
}
cout << endl;
}
cout << endl;
//2nd pattren
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
//3rd pattern
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
cout << " ";
}
for (int s = 1; s <= i; s++)
{
cout << "*";
49
}
cout << endl;
}
cout << endl;
//4th pattern
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
cout << " ";
}
for (int s = i; s <= 5; s++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
Output:
50
Task-7
Write a program that perform the following
series according to user demand.
1+2+3+4+6+.....+n
1*2*3*4*.......*n.
Code:
#include <iostream>
using namespace std;
int main() {
51
int n, i;
cout << "Enter the number untill which you want to run
the series = ";
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << i << "+";
}
cout << "......+n";
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << i << "*";
}
cout << "......*n";
Output:
Task-8
write a program that reverses the number e.g.,
4532 convert to 2354.
52
Code:
#include <iostream>
using namespace std;
int main() {
int n, remainder, reverse;
cout << "Enter tne number = ";
cin >> n;
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
cout << "The reverse number is = " << reverse << endl;
}
Output:
Task-9
Write a program that check whether the given
number is palindrome or not.
Code:
53
#include <iostream>
using namespace std;
int main()
{
int num, reverse = 0, remainder, swap;
cout << "Enter number to check = ";
cin >> num;
swap = num;
while (swap != 0)
{
remainder = swap % 10;
reverse = reverse * 10 + remainder;
swap /= 10;
}
if (num == reverse)
cout << "It is Palindrome";
else
cout << "It is not a Palindrome";
Output:
54
Task-10
print the table, ask the user which one they
want to show.
2*1=2
2*2=4
Code:
#include <iostream>
using namespace std;
int main()
{
int number, size;
cout << "Enter the number of which you want the table of
= ";
cin >> number;
cout << "Enter the size of the table = ";
cin >> size;
cout << "The table of {" << number << "} is " << endl;
for (int i = 1; i <= size; i++)
{
int product = number * i;
cout << number << " * " << i << " = " << product << endl;
}
55
}
Output:
Week: 07
Task-1
Write a C++ program that uses a while
loop to calculate and print the sum of
the even and odd integers from 2 to
30.
Code:
#include <iostream>
using namespace std;
int main()
56
{
int i = 2;
int e_num = 0;
int o_num = 0;
while (i <= 30)
{
if (i % 2 == 0)
{
e_num += i;
i++;
}
else
{
o_num += i;
i++;
}
}
cout << "Sum of even numbers is: " << e_num << endl;
cout << "Sum of odd numbers is: " << o_num;
}
Output:
Task-2
57
Write a program that reads in a four-
digit number, separates the number
into its individual digits and prints them
separated by three spaces. Thus given
the number 4233, the program should
print:
4 2 3 3
Code:
#include <iostream>
int main() {
int number;
cout << "Enter a four-digit number: ";
cin >> number;
int thousands = number / 1000;
int remainder = number % 1000;
int hundreds = remainder / 100;
remainder %= 100;
int tens = remainder / 10;
int units = remainder % 10;
58
cout << thousands << " " << hundreds << " " << tens <<
" " << units << endl;
}
Output:
Task-3
Write a C++ program that will
determine if a department store
Customer has exceeded the Credit limit
on a Charge account. For each
Customer, the following facts are
available:
1. Account number
2. Balance at beginning of month
3. Total of all items Charged by the
Customer this month
4. Total of all Credits applied to the
account this month
5. Allowed Credit limit
59
The program should input each of
these facts, Calculate the new
balance (initial balance + Charges -
Credits) and determine if the new
balance exceeds the Credit limit. If it
does, a suitable message should be
displayed.
Enter account number (-1 to end): 100
Enter initial balance: 5394.78
Enter total Charges : 1000
Enter total Credit: 500
Account: 100
Enter Credit Limit: 5500
Balance: 5894.78
Code:
#include <iostream>
int main() {
60
int ACNT_N0;
61
cout << "Account = " << ACNT_N0 << endl;
cout << "Balance = " << newBalance << endl;
Output:
62