0% found this document useful (0 votes)
27 views5 pages

CS100 Lab03 Pre-Lab-1

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)
27 views5 pages

CS100 Lab03 Pre-Lab-1

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/ 5

LAB 3

ARITHMETIC OPERATORS
1.1 OBJECTIVES
• Understand the different arithmetic operators in C++ (addition, subtraction,
multiplication, division, and modulus) and their syntax.
• Learn how to apply arithmetic operators in various programming scenarios with
practical examples.
• Explore the concept of constant variables in C++ and how to declare and use them.
• Develop the ability to write C++ programs that effectively use arithmetic operations and
constants to solve real-world problems.
1.2 PRE-LAB READING

Arithmetic operators in C++ are used to perform basic mathematical operations such as
addition, subtraction, multiplication, division, and modulus. These operators are fundamental
in programming and are used extensively in various applications. Understanding how arithmetic
operators work is essential for any C++ programmer. Arithmetic operators are symbols that
perform specific mathematical operations between operands. In C++, the following arithmetic
operators are commonly used:

1. Addition (`+`): Adds two operands.

2. Subtraction (`-`): Subtracts the second operand from the first.

3. Multiplication (`*`): Multiplies two operands.

4. Division (`/`): Divides the first operand by the second. If both operands are integers, the
result is an integer.

5. Modulus (`%`): Returns the remainder when the first operand is divided by the second. This
operator is only applicable to integers.

1.2.1 ADDITION (`+`)

The addition operator `+` is used to add two numbers. Below is an example demonstrating the
use of the addition operator in C++:
#include <iostream>
using namespace std;

int main() {
int a = 5, b = 10;
int sum = a + b;
cout << "Sum of a and b is: " << sum << endl;
return 0;
}

Output:

Explanation: In this program, two integers `a` and `b` are declared and initialized to 5 and 10,
respectively. The `+` operator is used to add `a` and `b`, and the result is stored in the variable
`sum`. The result is then printed to the console.

1.2.2 SUBTRACTION (`-`)

The subtraction operator `-` is used to subtract one number from another. Below is an example
demonstrating the use of the subtraction operator in C++:

#include <iostream>
using namespace std;

int main() {
int a = 15, b = 5;
int difference = a - b;
cout << "Difference between a and b is: " << difference << endl;
return 0;
}

Output:
Explanation: This program declares two integers `a` and `b` with values 15 and 5, respectively.
The `-` operator is used to subtract `b` from `a`, and the result is stored in the variable
`difference`. The result is then printed to the console.

1.2.3 MULTIPLICATION (`*`)

The multiplication operator `*` is used to multiply two numbers. Below is an example
demonstrating the use of the multiplication operator in C++:

#include <iostream>
using namespace std;

int main() {
int a = 7, b = 6;
int product = a * b;
cout << "Product of a and b is: " << product << endl;
return 0;
}

Output:

Explanation: In this program, two integers `a` and `b` are initialized to 7 and 6, respectively. The
`*` operator multiplies `a` and `b`, and the result is stored in the variable `product`. The result is
then displayed on the console.

1.2.4 DIVISION (`/`)

The division operator `/` is used to divide one number by another. Below is an example
demonstrating the use of the division operator in C++:
#include <iostream>
using namespace std;

int main() {
int a = 20, b = 4;
int quotient = a / b;
cout << "Quotient of a divided by b is: " << quotient << endl;
return 0;
}

Output:

Explanation: This program declares two integers `a` and `b` with values 20 and 4, respectively.
The `/` operator divides `a` by `b`, and the result is stored in the variable `quotient`. The result is
then printed to the console.

1.2.5 MODULUS (`%`)

The modulus operator `%` is used to find the remainder when one number is divided by
another. Below is an example demonstrating the use of the modulus operator in C++:

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 3;
int remainder = a % b;
cout << "Remainder when a is divided by b is: " << remainder << endl;
return 0;
}

Output:
Explanation: In this program, two integers `a` and `b` are initialized to 10 and 3, respectively.
The `%` operator computes the remainder of `a` divided by `b`, and the result is stored in the
variable `remainder`. The result is then displayed on the console.

1.2.6 CONSTANTS

Constants are variables whose values cannot be changed once they have been assigned. In C++,
the `const` keyword is used to define a constant variable. Constants are used when you want to
make sure that a value does not get modified throughout the program.

#include <iostream>
using namespace std;

int main() {
const int PI = 3.14;
int radius = 5;
float area = PI * radius * radius;
cout << "Area of the circle is: " << area << endl;
return 0;
}

Output:

Explanation: In this example, `PI` is declared as a constant using the `const` keyword and
initialized to 3.14. Since `PI` is a constant, its value cannot be changed later in the program. The
program calculates the area of a circle using the formula `PI * radius * radius` and displays the
result.

You might also like