Itc Lab A25
Itc Lab A25
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:
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.
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:
By initializing variables, you provide them with an initial value that can be used for further
operations or computations.
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
How It Works:
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.
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):
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