[go: up one dir, main page]

0% found this document useful (0 votes)
36 views18 pages

PF Lab Manual

The document outlines a series of lab exercises focused on basic arithmetic operations and increment/decrement operators in C++. Each exercise includes objectives, required software, theory, algorithms, code explanations, and sample input/output. The exercises aim to reinforce fundamental programming concepts and demonstrate the use of various operators in C++.

Uploaded by

wasay8025
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)
36 views18 pages

PF Lab Manual

The document outlines a series of lab exercises focused on basic arithmetic operations and increment/decrement operators in C++. Each exercise includes objectives, required software, theory, algorithms, code explanations, and sample input/output. The exercises aim to reinforce fundamental programming concepts and demonstrate the use of various operators in C++.

Uploaded by

wasay8025
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/ 18

1

Lab manual:
Lab exercise 01(Title): Basic Addition and subtraction program in C++.

Objective:
To understand and important a simple C++ program that takes two integer inputs from the user and perform
addition and subtraction , displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of cin for taking input , cout for displaying output and arithmetic operations
in C++.

Algorithm:
1. Start the program.
2. Declare two integer (num1, num2).
3. Prompt the user to enter the first number and store it in “num1”.
4. Prompt the user to enter the first number and store it in “num2”
5. Perform addition: num1 + num2
6. Similarly perform subtraction: num1 – num2.
7. Display the result of the above mentioned operations.
8. End the program.

Code:
2

Explanation:
1. The program starts by including the iostream library for input and output operations.
2. The main() function is defined which serves as the point of the program.
3. Three integers num1 and num2 are declared.
4. The program prompts the user to enter the two integers which are read using cin.
5. The sum of num1 and num2 is computed and stored in c.
6. The result is displayed using cout.
7. The program terminates successfully with return 0.

Input\output :
Sample input:
Enter first number : 8

Enter second number: 9

Sample output:
Addition : 8+9 = 17

Subtraction : 8-9 = -1

Conclusion:
This lab exercise demonstrates the basic input\output operations in C++ and simple arithmetic computations. The
program successfully takes user input , performs addition and subtraction , displays the result. Reinforcing
fundamental programming concepts.

Lab exercise 02 (Title):


Basic Multiplication and Division program in C++.

Objective:
To understand and important a simple C++ program that takes two integer inputs from the user and perform
multiplication and subtraction , displays its result.
3

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of cin for taking input , cout for displaying output and arithmetic operations
in C++.

Algorithm:
1. Start the program.
2. Declare two integer (num1, num2).
3. Prompt the user to enter the first number and store it in “num1”.
4. Prompt the user to enter the first number and store it in “num2”
5. Perform multiplication : num1 * num2
6. Similarly perform division: num1\num2
7. Display the result of the above mentioned operations.
8. End the program.

Code:

Explanation:
1. The program starts by including the iostream library for input and output operations.
2. The main() function is defined which serves as the point of the program.
3. The two numbers num1 and num2 are declared.
4. The program prompts the user to enter the two integers which are read using cin.
5. The multiplication of num1 and num2 is computed and stored in it
6. Similarly the division of num1 and num2 is computed and stored in the program..
4

7. The result is displayed using cout.


8. The program terminates successfully with return 0.

Input\output :
Sample input:
Enter first number : 10

Enter second number:5

Sample output:
Multiplication : 10*5 = 50

Division : 10\5 = 2

Conclusion:
This lab exercise demonstrates the basic input\output operations in C++ and simple arithmetic computations. The
program successfully takes user input , performs addition and subtraction , displays the result. Reinforcing
fundamental programming concepts.

Lab exercise 03 (Title):


Finding the remainder of the division (modulus operator).

Objective:
To understand the simple C++ program that takes variable input from the user and perform division (modulus
operator) and displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations
5

• Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of modulus operator. It explains the use of cin for taking input , cout for
displaying output and performing its operations in C++.

Algorithm :
1. Start the program.
2. Declare two integer (num1, num2).
3. Prompt the user to enter the first number and store it in “num1”.
4. Prompt the user to enter the first number and store it in “num2”
5. Perform division (modulus operator): num1 % num2
6. Similarly perform the operation: num1%num2
7. Display the result of the above mentioned operations.
8. End the program.

Code:

Explanation:
1. The program starts by including the iostream library for input and output operations.
2. The main() function is defined which serves as the point of the program.
3. Assigned the values , a = 144 and b= 12.
4. Now 144 % 12 divides, giving a quotient of 12 and a remainder 0.
5. The modulus operator (%) returns 0, which is its remainder.

Input\output :
Sample input:
Enter first number : 144

Enter second number:12


6

Sample output:
Modulus operator : 144 % 12 = 0

Conclusion:
The modulus operator is used in C++ program to find the remainder after division. It is useful for checking divisibility,
alternating patterns and cyclic operations. The result of a % b is always the remainder when a is divided by b. it only
works with integer types in C++ (not floating-point numbers).

Lab exercise 04(Title):


Increment a number using the pre-increment operator program in C++.

Objective:
To understand and important a simple C++ program that takes variable input from the user and perform increment
using the pre-increment operator, displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of pre-increment operator (++i) increases the value of “i” before using it in
an expression. It increment first, then the updated value is returned. This ensures that any expression using ++i
works with the new , incremented value immediately

Algorithm:
1. Start the program and initialize a variable (i).
2. Apply the pre- increment operator (++i).
3. It will increase the value of “i” by 1 and return the updated value.
4. Display the result of the operator (++i).
7

5. End the program.

Code:

Explanation:
1. The program starts by including the iostream library for pre-increment operation.
2. The main() function is defined which serves as the point of the program.
3. In this program “num” is declared and initialized to 19.
4. ++num increments num by 1, so now num = 20.
5. The result is displayed using cout.
6. The program terminates successfully with return 0.

Input\output :
Sample input:
Enter number = 19

Sample output:
8

Pre-increment operation = 20

Conclusion:
This program successfully demonstrate the use of the pre- increment operator by increment the allocated value 19
to 20. It actually increment first and then update the return value.

Lab exercise 05 (Title):


Increment a number using the post-increment operator program in C++.

Objective:
To understand and important a simple C++ program that takes variable input from the user and perform increment
using the post-increment operator, displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of post-increment operator (i++) increases the value of “i” after using it in an
expression. First the updated value is returned and then it increments it. This ensures that any expression using
i++ works with the new , incremented value immediately

Algorithm:
1. Start the program and initialize a variable (i).
2. Apply the post- increment operator (i++).
3. It will update the value of “i” and then increase it by 1 .
4. Display the result of the operator (i++).
5. End the program.
9

Code:

Explanation:
1. The program starts by including the iostream library for pre-increment operation.
2. The main() function is defined which serves as the point of the program.
3. In this program “num” is declared and initialized to 245.
4. num++ increments num by 1, so now num = 246.
5. The result is displayed using cout.
6. The program terminates successfully with return 0.

Input\output :

Sample input:
Enter number = 245
10

Sample output:

Post-increment number= 246

Conclusion:
This program successfully demonstrate the use of the post-increment operator by increment the allocated value 245
to 246. It actually update the return value and then increment it.

Lab exercise 06(Title):


Decrement a number using the pre-decrement operator program in C++.

Objective:
To understand and important a simple C++ program that takes variable input from the user and perform decrement
using the pre-decrement operator, displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of pre-decrement operator (--i) increases the value of “i” before using it in an
expression. It decrement first, then the updated value is returned. This ensures that any expression using --i
works with the new , decremented value immediately

Algorithm:
1. Start the program and initialize a variable (i).
2. Apply the pre- decrement operator (--i).
3. It will decrease the value of “i” by 1 and return the updated value.
4. Display the result of the operator (--i).
5. End the program.

Code:
11

Explanation:
1. The program starts by including the iostream library for pre-decrement operation.
2. The main() function is defined which serves as the point of the program.
3. In this program “num” is declared and initialized to 353 .
4. --num decrements num by 1, so now num =352 .
5. The result is displayed using cout.
6. The program terminates successfully with return 0.

Input\output :
Sample input:
Enter number = 353

Sample output:
Pre-decrement number = 352

Conclusion:
This program successfully demonstrate the use of the pre- increment operator by increment the allocated value 19
to 20. It actually increment first and then update the return value.
12

Lab exercise 07(Title):


decrement a number using the post-decrement operator program in C++.

Objective:
To understand and important a simple C++ program that takes variable input from the user and perform increment
using the post-increment operator, displays its result.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory:
C++ is a high-level programming language that supports input and output operations using standard libraries.
This program demonstrate the use of post-decrement operator (i--) decreases the value of “i” after using it in an
expression. First the updated value is returned and then it decrements it. This ensures that any expression using
i++ works with the new , decremented value immediately

Algorithm:
1. Start the program and initialize a variable (i).
2. Apply the post- decrement operator (i--).
3. It will update the value of “i” and then increase it by 1 .
4. Display the result of the operator (i--).
5. End the program.

Code:

Explanation:
1. The program starts by including the iostream library for pre-decrement operation.
13

2. The main() function is defined which serves as the point of the program.
3. In this program “num” is declared and initialized to 363.
4. The num-- decrements num by 1, so now num = 362.
5. The result is displayed using cout.
6. The program terminates successfully with return 0.

Input\output :
Sample input:
Enter number = 363

Sample output:
Post-decrement number = 362

Lab exercise 08 (Title):


Demonstration of the difference between the pre-increment and the post-increment program with code in C++.

Objective:
To demonstrate the difference between the pre increment (++i) operators and post increment operators (i++) in
C++.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory :
In C++ the increment operators are used to increase the value of a varaiable by 1. They can be used in two ways:

1. Pre-increment(++i):
i. The value of “i” is incremented first then used in the expression.
2. Post-increment(i++):
i. The current value of “i” is used first then incremented.
14

Algorithm:
1. Start the program.
2. Initialize two integers two integers variables e.g a = , b = .
3. Apply pre-increment (++i) , increase by 1 first and use the updated value of it and print the result.
4. Apply post-increment (i++), use the current value of it print the result and then increase by 1.
5. Now print the final value of post-increment.
6. End the program.

Code:

Explanation:
In C++ the increment operators are used to increase the value of a variable by 1. They can be used in two ways as
follow; In Pre-increment(++i) the value of “i” is incremented first then used in the expression. In Post-
increment(i++) the current value of “i” is used first then incremented.

Input\output :
Sample input:
Enter the number a = 245

Enter the number b = 287

Sample output:
The final result of a= 246
15

The final result of b = 288

Lab exercise 09(Title):


Demonstration of the difference between the pre-increment and the post-increment program with code in C++.

Objective:
To demonstrate the difference between the pre decrement (--i) operators and post decrement operators (i--) in C++.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Theory :
In C++ the increment operators are used to decrease the value of a variable by 1. They can be used in two ways:

1. Pre-decrement(--i):
i. The value of “i” is decremented first then used in the expression.
2. Post-decrement(i--):
i. The current value of “i” is used first then decremented.

Algorithm:
1. Start the program.
2. Initialize two integers two integers variables e.g a = , b = .
3. Apply pre-decrement (--i) , decrease by 1 first and use the updated value of it and print the result.
4. Apply post-decrement (i--), use the current value of it print the result and then decrease by 1.
5. Now print the final value of post-decrement.
6. End the program.
16

Code:

Explanation:
In C++ the decrement operators are used to decrease the value of a variable by 1. They can be used in two ways as
follow; In Pre-decrement(++i) the value of “i” is decremented first then used in the expression. In Post-
decrement(i++) the current value of “i” is used first then decremented.

Input\output :

Sample input:
Enter the number a = 245

Enter the number b = 287

Sample output:
The final result of a= 244

The final result of b=286


17

Lab exercise 10(Title):


Calculate the sum of three numbers in C++.

Objective:

To take three numbers as input from the user and compute their sum using C++.

Software Required:
• A computer with a C++ compiler (such as Code::Blocks, DEV-C++, or GCC)
• Basic knowledge of C++ syntax and input\output operations

Algorithm:

1. Start the program.


2. Declare three variables (num1, num2, num3) and one sum variable.
3. Take input for the three numbers.
4. Compute the sum using sum = num1 + num2 + num3.
5. Display the result.
6. End the program.

Code:

Explanation:

• The program uses cin to take input and cout to display the sum.
• The + operator adds the three numbers.
18

• The final result is printed using cout.

Inout\Output:

Sample input:

Enter three numbers: 74, 75, 76

Sample output:
Sum of three numbers : 74+75+76 = 225

You might also like