Introduction to Computing – Lab
Faculty of Information Technology & Computer Science
Lab 03
Course Instructor: Fraz Aslam
Topics: Compiling and running a program, Syntax of C++, Arithmetic
Expressions, Input & Output (using cin and cout), Variables
Instructions:
• Create separate C++ source files for each task, named as “task1.cpp”, “task2.cpp”, and so on
depending on the task number.
• After completing all tasks, place all .cpp files into a folder. Name the folder with your
university registration number (e.g., L1F20BSCS0999). Compress the folder into a .zip file
and upload it on the portal.
• Ensure that the work you submit is entirely your own. Avoid copying from peers, online
sources, or any other unauthorized material. Plagiarism will not be tolerated.
• If you encounter difficulties, feel free to reach out to the instructor. Collaboration and
discussion are encouraged, but the final implementation should be your own work.
• Write clean and well-structured code. Use comments to explain key sections of your code to
make it easier for others (and yourself) to understand.
• These tasks are designed to help you strengthen your logical thinking and problem-solving
skills. Think through each problem carefully before starting to code. The aim is to develop a
deep understanding of the problem and to devise solutions independently.
• Learning programming is about practice and perseverance—genuine effort in solving the
problems will contribute significantly to your learning.
Compiling & Running a Program
C++ Variables
In C++, variables are used to store data values that can change during the execution of a
program. Each variable has a specific type, which defines the kind of data it can hold, such as
integers, floating-point numbers, or characters.
cout (Output in C++)
cout is used in C++ to print output to the console. It belongs to the C++ Standard Library and is
defined in the iostream header.
The << is called the "insertion operator" and is used to send data to the output stream (cout).
You can output multiple items by chaining them with <<.
cin (Input in C++)
cin is used to get input from the user. It reads data from standard input and stores it in variable.
The >> is called the "extraction operator" and is used to receive data from the input stream
(cin). Make sure the variable types match the type of data input by the user.
Task 1
Write a C++ program that asks the user to input two numbers and then outputs their sum,
difference, product, and quotient.
• Hint: Use +, -, *, and / operators.
• Example Input: 5 3
• Example Output:
Sum: 8
Difference: 2
Product: 15
Quotient: 1.66667
Task 2
Write a program that converts temperature from Celsius to Fahrenheit.
• Formula: F = (9/5)*C + 32
• Example Input: 25 (Celsius)
• Example Output: 77 (Fahrenheit)
Task 3
Create a program that asks the user for the radius of a circle and calculates its area.
• Formula: Area = π*r*r (Use 3.14159 as the value for π).
• Example Input: 3
• Example Output: 28.27431
Task 4
Write a program that swaps the values of two variables.
• Hint: Use a third variable to swap.
• Example Input:
Enter first number: 10
Enter second number: 20
• Example Output:
After swapping, first number = 20
After swapping, second number = 10
Task 5
Write a program that takes an integer as input and outputs the remainder when divided by 2, 3,
and 5.
• Hint: Use modulus operator (%).
• Example Input: 17
• Example Output:
When divided by 2, remainder = 1
When divided by 3, remainder = 2
When divided by 5, remainder = 2
Task 6
Write a program that asks the user to input their age in years and calculates their age in days.
• Hint: Assume one year has 365 days.
• Example Input: 20
• Example Output: You are approximately 7300 days old.
Task 7
Create a program that takes an input in minutes and converts it to hours and minutes.
• Example Input: 130
• Example Output: 130 minutes = 2 hours and 10 minutes.
Task 8
Write a program that takes the length and width of a rectangle and calculates both its
perimeter and area.
• Formula: Perimeter = 2 * (length + width), Area = length * width
• Example Input: 5 3
• Example Output:
Perimeter: 16
Area: 15
Task 9
Write a program that calculates simple interest based on user input.
• Formula: Simple Interest = (A*R*T)/100 where A is the amount, R is the rate of interest, and
T is the time in years.
• Example Input: 1000 5 2
• Example Output: Simple Interest = 100
Task 10
Write a program that takes a two-digit number as input and prints the sum of its digits.
• Example Input: 45
• Example Output: The sum of digits is 9.