SOP 1st Lab Experiment
SOP 1st Lab Experiment
Venue :
Title of Experiment :
Date of Conduction :
Date of Submission :
Marks
Particulars Max. Marks
Obtained
Pre Lab 05
Lab Performance 05
Post Lab 05
05
Record Submission
20
Total
REPORT VERIFICATION
Staff Name :
Signature :
1
Experiment No. 1
int main() {
// Declare variables
int a = 10, b = 5;
bool x = true, y = false;
// Arithmetic operators
cout << "Arithmetic Operators:" << endl;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;
// Relational operators
cout << "\nRelational Operators:" << endl;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
// Logical operators
cout << "\nLogical Operators:" << endl;
cout << "x && y: " << (x && y) << endl;
cout << "x || y: " << (x || y) << endl;
cout << "!x: " << (!x) << endl;
// Bitwise operators
cout << "\nBitwise Operators:" << endl;
cout << "a & b: " << (a & b) << endl;
cout << "a | b: " << (a | b) << endl;
cout << "a ^ b: " << (a ^ b) << endl;
cout << "~a: " << (~a) << endl;
cout << "a << 1: " << (a << 1) << endl;
2
cout << "a >> 1: " << (a >> 1) << endl;
// Assignment operators
cout << "\nAssignment Operators:" << endl;
int c = a;
cout << "c = " << c << endl;
c += b; cout << "c += b: " << c << endl;
c -= b; cout << "c -= b: " << c << endl;
c *= b; cout << "c *= b: " << c << endl;
c /= b; cout << "c /= b: " << c << endl;
c %= b; cout << "c %= b: " << c << endl;
c &= b; cout << "c &= b: " << c << endl;
c |= b; cout << "c |= b: " << c << endl;
c ^= b; cout << "c ^= b: " << c << endl;
c <<= 1; cout << "c <<= 1: " << c << endl;
c >>= 1; cout << "c >>= 1: " << c << endl;
// Ternary operator
cout << "\nTernary Operator:" << endl;
cout << "(a > b ? a : b): " << (a > b ? a : b) << endl;
return 0;
}
Program Output
Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0
Relational Operators:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
3
a <= b: 0
Logical Operators:
x && y: 0
x || y: 1
!x: 0
Bitwise Operators:
a & b: 0
a | b: 15
a ^ b: 15
~a: -11
a << 1: 20
a >> 1: 5
Assignment Operators:
c = 10
c += b: 15
c -= b: 10
c *= b: 50
c /= b: 10
c %= b: 0
c &= b: 0
c |= b: 5
c ^= b: 0
c <<= 1: 0
c >>= 1: 0
Ternary Operator:
(a > b ? a : b): 10
4
Simulation Output
5
6
Program 2: Write a program in C++ to demonstrate all conditional statements.
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
// Simple if statement
if (number > 0) {
cout << "The number is positive." << endl;
}
// If-else statement
if (number % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}
// Nested if statement
if (number != 0) {
if (number > 0) {
cout << "The number is positive and non-zero." << endl;
} else {
cout << "The number is negative and non-zero." << endl;
}
}
// Switch statement
cout << "Enter a choice (1-3): ";
int choice;
cin >> choice;
switch (choice) {
7
case 1:
cout << "You selected option 1." << endl;
break;
case 2:
cout << "You selected option 2." << endl;
break;
case 3:
cout << "You selected option 3." << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}
return 0;
}
Program Output
Enter a number: 8
The number is positive.
The number is even.
The number is positive.
The number is positive and non-zero.
Enter a choice (1-3): 2
You selected option 2.
8
SimulationOutput
9
10
11
Program 3: Write a program in C++ to demonstrate References and Pointers
int main() {
int num = 20;
// Demonstrating pointers
int *ptr = # // Pointer stores the address of num
cout << "Using Pointers:" << endl;
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value using pointer: " << *ptr << endl;
cout << "Pointer address: " << ptr << endl;
modifyWithPointer(ptr);
cout << "Value of num after function call via pointer: " << num << endl;
// Demonstrating references
int &ref = num; // Reference to num
cout << "\nUsing References:" << endl;
cout << "Value of num: " << num << endl;
cout << "Reference value: " << ref << endl;
modifyWithReference(ref);
cout << "Value of num after function call via reference: " << num << endl;
return 0;
}
Program Output
Using Pointers:
Value of num: 20
Address of num: 0x7fff191b682c
Value using pointer: 20
Pointer address: 0x7fff191b682c
Value of num after modification via pointer: 30
Value of num after function call via pointer: 40
Using References:
Value of num: 40
Reference value: 40
Value of num after modification via reference: 40
Value of num after function call via reference: 50
13
Simulation Output
14
15
Pre lab Questions:
1. What are the different data types present in C++?
2. What is the difference between C and C++?
3. What is polymorphism in C++?
4. What is the use of pointers?
5. How do references help in simplifying the code?
Post lab Questions:
1. Why do we include namespace std?
2. Differentiate between cout and cin functions.
3. Why do we use return 0 at the end of main function
4. Suppose, we want a pointer varPoint to point to the address of var. Then, which of the
following is correct. If wrong, give the reason and correct all the wrong statements.
16
Experiment No.3
Aim / Objective: To write and execute C++ programs to demonstrate the use functions.
Simulate the program in C++ compiler.
int main() {
int num;
// 1. If statement
if (num > 0) {
cout << "The number is positive." << endl;
}
// 2. If-Else statement
if (num % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}
// 3. If-Else If ladder
if (num > 0) {
cout << "The number is positive." << endl;
} else if (num < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
// 4. Switch statement
cout << "\nEnter a day number (1-7): ";
int day;
cin >> day;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
17
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day number!" << endl;
}
return 0;
}
Program Output
Enter a number: 2
The number is positive.
The number is even.
The number is positive.
18
Simulation Output
19
20
21
Experiment No.2
Aim / Objective: To write and execute C++ programs to demonstrate the use functions.
Simulate the program in C++ compiler.
swap(x, y);
return 0;
}
Program Output
Before Swapping: x: 1, y: 2
After Swapping in function x: 2, y: 1
After Swapping: x: 1, y: 2
22
Simulation Output
23
Program 2: Write a C++ program using functions (Call by reference method)
//RA2411043010041 Shahishnu Rahul
#include <iostream>
using namespace std;
void swapNumbers(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
swapNumbers(num1, num2);
cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
}
Program Output
Enter two numbers: 1 2
Before swapping: num1 = 1, num2 = 2
After swapping: num1 = 2, num2 = 1
24
Simulation Output
25
Write a program for npr factorial
26
27
Pre lab Questions
1. Why do we need functions?
2. What are function arguments?
3. What is function overloading?
4. Can we have a function with no return value?
Post lab questions
1. Differentiate between user defined and library functions with examples.
2. What is the return value for main()?
3. What is difference in precision between float and double data type?
28