0% found this document useful (0 votes)
9 views

Itc Lab A25

Uploaded by

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

Itc Lab A25

Uploaded by

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

Introduction to Computing — Lab

Section: A25 Lab Instructor: Javaria Tanveer

Topic
Introduction to C++ Input/Output and Basic Arithmetic Operations and Understanding "If-Else"
Statements in C++

Objective
 To understand the use of cin and cout for input and output operations in C++.
 To learn and apply basic arithmetic operations (addition, subtraction, multiplication, and
division) using C++ operators.
 To explore output formatting techniques using special characters like \n (newline) and \t
(tab).
 To understand the importance of variable initialization and working with different data
types like int, float, and bool in C++.
 To learn and understand the usage of conditional statements (if, else, else if) in C++
programming.

Outcomes
 Students will be able to take user input and display output using cin and cout in C++.
 Students will successfully implement basic arithmetic operations and display the results
clearly using formatting techniques.
 Students will understand how to initialize variables of different data types (int, float, and
bool) and apply them in their programs for basic computations.
 Students will gain experience in organizing output with appropriate formatting for clarity
and readability.
 Students will be able to use if, else, and else if to solve various problems like number
classification, eligibility checks, temperature categorization, etc.

Content
The following topics require revision for this lab session:
1. cin, cout
2. Basic Arithmetic Operations
3. If-else statement
Details of these topics are given below:

Introduction to C++ Input/Output and Basic Arithmetic Operations

In C++, one of the most fundamental operations is taking input from the user and displaying the
output. This is typically done using the cin (console input) and cout (console output) objects, which

Page 1 of 4
are part of the iostream library. Along with input and output, C++ also allows you to perform
arithmetic operations such as addition, subtraction, multiplication, and division using basic
operators like +, -, *, and /.

When writing code, formatting the output is important for clarity. The special characters \n
(newline) and \t (tab) are used to format the output in a readable manner. These formatting
techniques help present the results neatly, making it easier for the user to understand.

In this series of tasks, you will practice basic input/output operations, simple arithmetic
calculations, and variable initialization with different data types such as int, float, and bool.

Introduction to Variable Initialization

In C++, variables are used to store data such as numbers or values. You must declare a variable
before using it and initialize it with a value. C++ supports various data types, and the most
commonly used types include:

1. int: Used to store integers (whole numbers).


2. float: Used to store decimal numbers.
3. bool: Used to store Boolean values (true or false).

By initializing variables, you provide them with an initial value that can be used for further
operations or computations.

"If-Else" Statements in C++


In C++, the if-else statement is a fundamental conditional structure that allows you to execute
certain blocks of code based on whether a condition is true or false. It helps your program make
decisions and follow different paths depending on the conditions you specify. The basic syntax
of the if-else statement is as follows:

if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
How It Works:

 if block: This block of code runs if the condition is true.


 else block: This block runs if the condition is false.

You can also use multiple else if statements to check for more than two conditions, allowing your
program to choose among several possible actions.

Page 2 of 4
Task 1:

Convert the following pseudocode to C++ code. Be sure to define the appropriate variables.

 Store 20 in the speed variable.


 Store 10 in the time variable.
 Multiply speed by time and store the result in the distance variable.
 Display the contents of the distance variable.

Task 2:

You are designing a store checkout system. Take the price of two items (floating point
numbers) as input and display the total cost, ensuring that there’s a tab space between the
price of each item and the total.

Task 3:

You are working on a bill calculation system. Take the original bill amount and a discount
percentage (both integers) from the user and calculate the discounted price, displaying the
result on a new line.

Task 4:

A car holds 12 gallons of gasoline and can travel 350 miles before refueling. Write a program
that calculates the number of miles per gallon the car gets. Display the result on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG):

MPG = Miles Driven / Gallons of Gas Used

Task 5:

You are building a weather app. Initialize a float variable to store the current temperature,
assign it a value, and display it.

Task 6:

You are creating a membership system for a website. Initialize a boolean variable to track
whether a user is subscribed or not, assign it a value, and display it (show 1 for true or 0 for
false).

Page 3 of 4
Task 7:

You are building an event management system. Initialize an integer variable for the number of
attendees, a float variable for ticket price, and a boolean variable to check if the event is sold out,
assigning appropriate values and displaying all three variables.

Task 8:

Write a program that takes an integer as input from the user and checks if it is divisible by 5. If it
is divisible by 5, display "Divisible by 5," otherwise display "Not divisible by 5."

Task 9:

Write a program that takes the user's age as input and checks if they are eligible to vote. If the
age is 18 or above, display "Eligible to Vote," otherwise display "Not Eligible to Vote."

Task 10:

Write a program that takes an integer as input and checks whether the number is even or odd. If
the number is even, display "Even," otherwise display "Odd."

Task 11:

Write a program that checks if a number is between 10 and 50 (inclusive). If the number is in this
range, display "Number is between 10 and 50," otherwise display "Number is out of range."

Page 4 of 4

You might also like