Beginner-friendly guide to C++ one-dimensional arrays: declaration, initialization, indexing, accessing elements, loops, and practical examples.
By the end of this lab, students will be able to:
if statements.Arithmetic operators are used to perform mathematical calculations.
| Operator | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Remainder |
#include <iostream>
using namespace std;
int main() {
int a = 20;
int b = 6;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Remainder: " << a % b << endl;
return 0;
}
Addition: 26
Subtraction: 14
Multiplication: 120
Division: 3
Remainder: 2
a + b
adds two numbers.
a - b
subtracts b from a.
a * b
multiplies two numbers.
a / b
performs division.
a % b
gives the remainder after division.
For example:
20 / 6 = 3
20 % 6 = 2
Change:
int a = 20;
int b = 6;
to:
int a = 50;
int b = 8;
Run the program and observe the results.
Try the program with:
a = 100, b = 15
a = 25, b = 4
a = 75, b = 10
Write a program that takes two integers from the user and displays:
An expression is a combination of values, variables, and operators that produces a result.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 5;
int result = a + b * 2;
cout << "Result = " << result;
return 0;
}
Result = 20
The expression is:
a + b * 2
Multiplication is performed before addition.
5 * 2 = 10
10 + 10 = 20
Change:
int result = a + b * 2;
to:
int result = (a + b) * 2;
Observe the difference.
Write a program that calculates:
total = price + tax
Use:
price = 1000
tax = 150
Display the total.
The assignment operator = is used to assign a value to a variable.
#include <iostream>
using namespace std;
int main() {
int marks = 75;
cout << "Marks = " << marks;
return 0;
}
int marks = 75;
creates a variable named marks and assigns 75 to it.
The = operator means:
Store the value on the right side in the variable on the left side.
Change:
int marks = 75;
to:
int marks = 85;
Run the program.
Create variables for:
studentAge
semester
marks
Assign suitable values and display them.
Compound assignment operators provide a shorter way to update a variable.
| Operator | Example | Equivalent |
|---|---|---|
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 5 |
x = x % 5 |
#include <iostream>
using namespace std;
int main() {
int x = 20;
x += 10;
cout << "After += : " << x << endl;
x -= 5;
cout << "After -= : " << x << endl;
x *= 2;
cout << "After *= : " << x << endl;
x /= 5;
cout << "After /= : " << x << endl;
return 0;
}
When we write:
x += 10;
it means:
x = x + 10;
If x is 20:
20 + 10 = 30
So x becomes 30.
Change:
int x = 20;
to:
int x = 100;
Change the values used with +=, -=, *=, and /=.
Observe the results.
Start with:
int balance = 1000;
Perform these operations:
50020024Use compound assignment operators.
Display the balance after every operation.
++The increment operator increases a value by 1.
#include <iostream>
using namespace std;
int main() {
int students = 30;
students++;
cout << students;
return 0;
}
31
Before:
students = 30
After:
students++;
the value becomes:
students = 31
The following two statements have the same effect when used alone:
students++;
and:
students = students + 1;
Change:
int students = 30;
to:
int students = 50;
Run the program.
Create:
int books = 10;
Use ++ to increase the number of books by one.
Display the result.
--The decrement operator decreases a value by 1.
#include <iostream>
using namespace std;
int main() {
int seats = 50;
seats--;
cout << seats;
return 0;
}
49
seats--;
is equivalent to:
seats = seats - 1;
Change:
int seats = 50;
to:
int seats = 100;
Run the program.
Create:
int lives = 5;
Use -- twice and display the final value.
There are two forms of increment:
++x
and:
x++
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << x++ << endl;
cout << x << endl;
return 0;
}
5
6
With:
x++
the current value is used first, and then the value is increased.
Now compare it with:
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << ++x << endl;
cout << x << endl;
return 0;
}
6
6
With:
++x
the value is increased first, and then the new value is used.
Change:
int x = 5;
to:
int x = 10;
Predict the output before running the program.
Write a program that demonstrates the difference between:
x++;
and:
++x;
Use x = 10.
Record the output of both programs.
Relational operators are used to compare values.
| Operator | Meaning |
|---|---|
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
== |
Equal to |
!= |
Not equal to |
#include <iostream>
using namespace std;
int main() {
int a = 20;
int b = 10;
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a == b) << endl;
return 0;
}
1
0
0
In C++:
1 = true
0 = false
a > b
asks:
Is a greater than b?
Because:
20 > 10
is true, the result is:
1
Change:
int a = 20;
int b = 10;
to:
int a = 5;
int b = 10;
Predict the output before running the program.
Take two numbers from the user and display the result of:
>
<
>=
<=
==
!=
>=#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter marks: ";
cin >> marks;
if (marks >= 50) {
cout << "Pass";
}
return 0;
}
The condition:
marks >= 50
checks whether the marks are greater than or equal to 50.
If the condition is true, the program displays:
Pass
Change the passing marks from:
50
to:
40
Test the program again.
Write a program that checks whether a student has obtained 60 or more marks.
Display:
Qualified
if the condition is true.
&&The && operator is used when all conditions must be true.
#include <iostream>
using namespace std;
int main() {
int age = 20;
int marks = 70;
if (age >= 18 && marks >= 50) {
cout << "Eligible";
}
return 0;
}
The program checks two conditions:
age >= 18
and:
marks >= 50
Both must be true.
Age = 20 → true
Marks = 70 → true
Therefore:
Eligible
Change:
int marks = 70;
to:
int marks = 40;
Run the program.
A student is eligible for an exam if:
7550Write a program using &&.
||The || operator is used when at least one condition must be true.
#include <iostream>
using namespace std;
int main() {
int semester;
cout << "Enter semester: ";
cin >> semester;
if (semester == 1 || semester == 2) {
cout << "Junior Semester";
}
return 0;
}
The student belongs to the junior semesters if:
Semester = 1
OR
Semester = 2
Only one condition needs to be true.
Change the condition so that semesters 1, 2, or 3 are considered junior semesters.
Write a program that checks whether a student is eligible for a special lab if they are in:
Use ||.
!The ! operator reverses a Boolean value.
#include <iostream>
using namespace std;
int main() {
bool isStudent = true;
cout << !isStudent;
return 0;
}
0
The original value is:
true
The ! operator changes it to:
false
C++ displays false as 0.
Change:
bool isStudent = true;
to:
bool isStudent = false;
Observe the result.
Create:
bool isLoggedIn = false;
Use ! to display the opposite value.
Different operators can be used together in one program.
#include <iostream>
using namespace std;
int main() {
int age;
int marks;
cout << "Enter age: ";
cin >> age;
cout << "Enter marks: ";
cin >> marks;
if (age >= 18 && marks >= 50) {
cout << "Student is eligible";
} else {
cout << "Student is not eligible";
}
return 0;
}
This program uses:
>>&&ifelseThe important condition is:
age >= 18 && marks >= 50
Both conditions must be true.
Add attendance:
int attendance;
Then change the condition to:
age >= 18 && marks >= 50 && attendance >= 75
Write a program that checks whether a student can sit in the final examination.
Conditions:
Attendance >= 75
AND
Marks >= 50
AND
Fee Paid = Yes
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter marks: ";
cin >> marks;
if (marks >= 50) {
cout << "Pass";
} else {
cout << "Fail";
}
return 0;
}
The program takes marks from the user.
If:
marks >= 50
is true, it displays:
Pass
Otherwise:
Fail
Add grades:
80 or above → A
70–79 → B
60–69 → C
50–59 → D
Below 50 → Fail
Write a complete student result program that takes:
Student Name
Marks
and displays:
Student Name
Marks
Result
Grade
#include <iostream>
using namespace std;
int main() {
double price;
int quantity;
cout << "Enter price: ";
cin >> price;
cout << "Enter quantity: ";
cin >> quantity;
double total = price * quantity;
cout << "Total Bill = " << total << endl;
return 0;
}
The expression:
price * quantity
calculates the total bill.
For example:
Price = 500
Quantity = 4
Total = 500 × 4
= 2000
Add a 10% discount if the total bill is 5000 or more.
Hint:
if (total >= 5000)
Write a complete shopping bill program.
The program should:
5000 or more.Before running each program, predict the output.
int a = 10;
int b = 5;
cout << a + b;
Output:
__________
int x = 10;
x += 5;
cout << x;
Output:
__________
int x = 5;
cout << x++;
cout << x;
Output:
__________
int x = 5;
cout << ++x;
cout << x;
Output:
__________
int a = 10;
int b = 20;
cout << (a < b);
Output:
__________
int age = 20;
int marks = 70;
cout << (age >= 18 && marks >= 50);
Output:
__________
Complete the following table.
| Expression | Your Prediction | Actual Result | ||
|---|---|---|---|---|
10 + 5 |
||||
10 - 5 |
||||
10 * 5 |
||||
10 / 5 |
||||
10 % 3 |
||||
10 > 5 |
||||
10 < 5 |
||||
10 == 10 |
||||
10 != 10 |
||||
10 > 5 && 5 > 2 |
||||
10 < 5 | | 5 > 2 |
Students should solve these tasks without looking at the examples.
Create a calculator that takes two numbers and displays:
Take an integer from the user.
Use % to check whether the number is:
Even
or:
Odd
Hint:
number % 2
Take the user’s age.
If age is 18 or above, display:
Adult
Otherwise display:
Minor
Take two numbers.
Display whether:
Take:
Age
Marks
Attendance
A student is eligible when:
Age >= 18
Marks >= 50
Attendance >= 75
Use:
&&
Take the shopping bill amount.
If the bill is:
5000 or more → 10% discount
3000–4999 → 5% discount
Below 3000 → No discount
Display:
Create a program that takes:
Age
Marks
Attendance
Semester
The student is eligible if:
Age >= 18
AND
Marks >= 50
AND
Attendance >= 75
Additionally, if the semester is 1 or 2, display:
Junior Semester
Otherwise display:
Senior Semester
The program should use:
&&||ifelseAnswer the following questions.
+?%?= and ==?++x and x++?--?>= mean?!= mean?&&?||?! do?x += 5 and x = x + 5?x *= 2 do?20 % 6?Students must submit: