0% found this document useful (0 votes)
44 views

COPROG

The document contains code and explanations for C++ programs that demonstrate the use of logical operators, arithmetic operators, and relational operators. The code samples take in user input, perform logical and mathematical comparisons on the input, and output results based on the operator evaluations.

Uploaded by

Kurt Atienza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

COPROG

The document contains code and explanations for C++ programs that demonstrate the use of logical operators, arithmetic operators, and relational operators. The code samples take in user input, perform logical and mathematical comparisons on the input, and output results based on the operator evaluations.

Uploaded by

Kurt Atienza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Valenzuela, Kim Harold

Atienza, Kurt Judd

Programming Fundamentals
(Logical Operators)
using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (num >= 10 && num <= 20) {

cout << "The number is between 10 and 20." << endl;

} else {

cout << "The number is not between 10 and 20." << endl;

ixf (num < 0 || num > 100) {

cout << "The number is less than 0 or greater than 100." << endl;

} else {

cout << "The number is between 0 and 100." << endl;

if (num % 2 == 0 && num % 3 == 0) {

cout << "The number is divisible by both 2 and 3." << endl;

} else {

cout << "The number is not divisible by both 2 and 3." << endl;

return 0;
}
Explanation of the code

1. The line #include <iostream> is a preprocessor directive that tells the compiler to include the
input/output stream library. This provides functionality for reading input and writing output.

2. The line using namespace std; is a using declaration that allows you to use names from the std
namespace without explicitly qualifying them. In this case, it allows you to use cout, cin, and endl
without writing std:: before them.

3. The main() function is the program entry point. Execution starts from here.

4. The line int num; declares an integer variable called num, which will store the input number provided
by the user.

5. The line cout << "Enter a number: "; outputs the prompt, asking the user to enter a number.

6. The line cin >> num; reads the user's input from the console and stores it in the num variable.

7. The first if statement checks if the number is between 10 and 20 (inclusive) using the logical AND
operator (&&). If the condition is positive, it runs the code inside the corresponding block, which displays
"The number is between 10 and 20." Otherwise, if the condition is negative, it runs the code inside the
else block, which displays "The number is not between 10 and 20."

8. The second if statement checks if the number is less than 0 or greater than 100 using the logical OR
operator (||). If the condition is true, it executes the code inside the corresponding block, which outputs
"The number is less than 0 or greater than 100." In contrast, if the condition is false, it executes the code
inside the else block, which outputs "The number is between 0 and 100."

9. The third if statement determines if the number is divisible by both 2 and 3 using the modulo operator
(%). If the condition is true, it executes the code inside the corresponding block, which outputs "The
number is divisible by both 2 and 3." Otherwise, if the condition is false, it executes the code inside the
else block, which outputs "The number is not divisible by both 2 and 3."

10. The return 0; statement terminates the main() function and returns 0 to the operating system,
showing successful program execution.

(Arithmetic Operators)

#include <iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter the first number: ";

cin >> num1;

cout << "Enter the second number: ";

cin >> num2;

int sum = num1 + num2;

cout << "Sum: " << sum << endl;

int difference = num1 - num2;

cout << "Difference: " << difference << endl;

int product = num1 * num2;

cout << "Product: " << product << endl;


if (num2 != 0) {

float quotient = static_cast<float>(num1) / num2;

cout << "Quotient: " << quotient << endl;

} else {

cout << "Cannot divide by zero." << endl;

int remainder = num1 % num2;

cout << "Remainder: " << remainder << endl;

return 0;

Explanation of the code

1. The line #include <iostream> is a preprocessor directive that tells the compiler to include the
input/output stream library. This provides functionality for reading input and writing output.

2. The line using namespace std; is a using declaration that allows you to use names from the std
namespace without explicitly qualifying them. In this case, it allows you to use cout, cin, and endl
without writing std:: before them.

3. The main() function is the program entry point. Execution starts from here.

4. The lines int num1, num2; declare two integer variables called num1 and num2, which will store the
input numbers provided by the user.

5. The line cout << "Enter the first number: "; outputs the prompt asking the user to enter the first
number.

6. The line cin >> num1; reads the user's input from the console and stores it in the num1 variable.

7. The line cout << "Enter the second number: "; outputs the prompt, asking the user to enter the
second number.

8. The line cin >> num2; reads the user's input from the console and stores it in the num2 variable.

9. The line int sum = num1 + num2; calculates the sum of num1 and num2 and stores it in the sum
variable.

10. The line cout << "Sum: " << sum << endl; outputs the sum by combining the string "Sum: " with the
value of sum.

11. The line int difference = num1 - num2; computes the difference between num1 and num2 and stores
it in the difference variable.

12. The line cout << "Difference: " << difference << endl; outputs the difference by combining the string
"Difference: " with the value of difference.

13. The line int product = num1 * num2; calculates the product of num1 and num2 and stores it in the
product variable.
14. The line cout << "Product: " << product << endl; outputs the product by combining the string
"Product: " with the product value.

15. The if statement checks if num2 is not equal to 0. If the condition is true, it executes the code inside
the corresponding block. It determines the quotient of num1 divided by num2 and saves it in the
quotient variable. It uses a static cast to convert the result to a float. It then outputs the quotient by
combining the string "Quotient: " with the value of the quotient. If the condition is false (i.e., num2 is 0),
it executes the code inside the else block, which outputs "Cannot divide by zero."

16. The line int remainder = num1 % num2; calculates the remainder when num1 is divided by num2
using the modulo operator % and stores it in the remainder variable.
17. The line cout << "Remainder: " << remainder << endl; outputs the remainder by combining the string
"Remainder: " with the value of remainder.

18. The return 0; statement terminates the main() function and returns 0 to the operating system,
indicating successful program execution.

(Relational Operators)

#include <iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter the first number: ";

cin >> num1;

cout << "Enter the second number: ";

cin >> num2;

if (num1 == num2) {

cout << "The numbers are equal." << endl;

} else {

cout << "The numbers are not equal." << endl;

if (num1 != num2) {

cout << "The numbers are not equal." << endl;

} else {
cout << "The numbers are equal." << endl;

if (num1 > num2) {

cout << num1 << " is greater than " << num2 << endl;

} else {

cout << num1 << " is not greater than " << num2 << endl;

if (num1 < num2) {

cout << num1 << " is less than " << num2 << endl;

} else {

cout << num1 << " is not less than " << num2 << endl;

if (num1 >= num2) {

cout << num1 << " is greater than or equal to " << num2 << endl;

} else {

cout << num1 << " is not greater than or equal to " << num2 << endl;

if (num1 <= num2) {

cout << num1 << " is less than or equal to " << num2 << endl;

} else {

cout << num1 << " is not less than or equal to " << num2 << endl;

}
return 0;

Explanation to the code

1. The line #include <iostream> is a preprocessor directive that tells the compiler to include the
input/output stream library, which provides functionality for reading input and writing output.

2. The line using namespace std; is a using declaration that allows you to use names from the std
namespace without explicitly qualifying them. In this case, it allows you to use cout, cin, and endl
without writing std:: before them.

3. The main() function is the entry point of the program. Execution starts from here.

4. The lines int num1, num2; declare two integer variables called num1 and num2, which will store the
input numbers provided by the user.

5. The line cout << "Enter the first number: "; outputs the prompt, asking the user to enter the first
number.

6. The line cin >> num1; reads the user's input from the console and stores it in the num1 variable.

7. The line cout << "Enter the second number: "; outputs the prompt, asking the user to enter the
second number.
8. The line cin >> num2; reads the user's input from the console and stores it in the num2 variable.

9. The first if statement checks if num1 is equal to num2 using the equality operator ==. If the condition
is true, it executes the code inside the corresponding block, which outputs "The numbers are equal."
Otherwise, if the condition is false, it executes the code inside the else block, which outputs "The
numbers are not equal."

10. The second if statement checks if num1 is not equal to num2 using the inequality operator !=. If the
condition is true, it executes the code inside the corresponding block, which outputs "The numbers are
not equal." Otherwise, if the condition is false, it executes the code inside the else block, which outputs
"The numbers are equal."

11. The third if statement checks if num1 is greater than num2 using the greater-than operator >. If the
condition is true, it executes the code inside the corresponding block, which outputs the message stating
that num1 is greater than num2. Otherwise, if the condition is false, it executes the code inside the else
block, which outputs the message stating that num1 is not greater than num2.

12. The fourth if statement checks if num1 is less than num2 using the less-than operator <. If the
condition is true, it executes the code inside the corresponding block, which outputs the message stating
that num1 is less than num2. Otherwise, if the condition is false, it executes the code inside the else
block, which outputs the message stating that num1 is not less than num2.

13. The fifth if statement checks if num1 is greater than or equal to num2 using the greater-than-or-
equal-to operator >=. If the condition is true, it executes the code inside the corresponding block, which
outputs the message stating that num1 is greater than or equal to num2. Otherwise, if the condition is
false, it executes the code inside the else block, which outputs the message stating that num1 is not
greater than or equal to num2.

14. The sixth if statement checks if `num

Control Structures
(while loop)

#include <iostream>

using namespace std;

int main() {

int count = 1;
while (count <= 5) {

cout << "Count: " << count << endl;

count++;

return 0;

Explanation of the code

1. The line #include <iostream> is a preprocessor directive that tells the compiler to include the
input/output stream library. This provides functionality for reading input and writing output.

2. The line using namespace std; is a using declaration that allows you to use names from the std
namespace without explicitly qualifying them. Here, it allows you to use cout and endl without writing
std:: before them.

3. The main() function is the program entry point. Execution starts from here.

4. The line int count = 1; declares an integer variable called count and initializes it with the value 1.

5. The while loop is a control structure that repeatedly executes a block of code as long as the condition
is true. Here, the condition is count <= 5, which means the loop will continue as long as count is less than
or equal to 5.

6.  Inside the while loop, the line cout << "Count: " << count << endl; outputs the current value of count
along with the string "Count: ". The << operator is used for concatenating values to be printed.

7. The line count++; increments the value of count by 1. This is equivalent to count = count + 1 or count
+= 1. It ensures that the value of count is updated with each loop iteration.

8. Once count becomes 6, the condition count <= 5 is no longer true, and the loop ends.

9. The return 0; statement terminates the main() function and returns 0 to the operating system,
showing successful program execution.

(for loop)
#include <iostream>

using namespace std;

int main() {

for (int count = 1; count <= 5; count++) {

cout << "Count: " << count << endl;

return 0;

1. The line #include <iostream> is a preprocessor directive that tells the compiler to include the
input/output stream library. This provides functionality for reading input and writing output.

2. The line using namespace std; is a using declaration that allows you to use names from the std
namespace without explicitly qualifying them. Here, it allows you to use cout and endl without writing
std:: before them.

3. The main() function is the program entry point. Execution starts from here.

4. The for loop is a control structure comprising three parts: initialization, condition, and increment.
Here, the initialization part is int count = 1;, which declares an integer variable called count and initializes
it with the value 1. The condition part is count <= 5;, which specifies that the loop will continue as long
as count is less than or equal to 5. The increment part is count++, which increments the value of count
by 1 after each iteration.

5. Inside the for loop, the line cout << "Count: " << count << endl; outputs the current value of count
along with the string "Count: ". The << operator is used for concatenating values to be printed.

6. The loop continues executing until the condition count <= 5 is false. Once count becomes 6, the loop
terminates.

7. The return 0; statement terminates the main() function and returns 0 to the operating system,
indicating successful program execution.

8. Overall, this program uses a for loop to iterate from 1 to 5 and outputs the value of count at each
iteration

(Do while loop)

#include <iostream>

using namespace std;

int main() {

int num;

do {

cout << "Enter a number greater than 10: ";

cin >> num;

} while (num <= 10);

cout << "The number you entered, " << num << ", is greater than 10. Exiting the loop." << endl;

return 0;

}
Explanation of the code

1. The #include <iostream> directive includes the necessary input/output stream library, allowing the
program to use input/output operations.

2. The line using namespace std; shows that the program will use the standard namespace. This includes
commonly used functions and objects from the C++ standard library, such as cout and cin.

3. The int main() function is the program entry point. It has a return type of int to indicate success or
failure. Inside the main() function, an integer variable num is declared but not initialized. It will be used
to store the user's input.

4. The do-while loop is initiated. It ensures that the block of code inside the loop is executed at least
once before checking the loop condition. Within the loop, the message "Enter a number greater than 10:
" is displayed using cout, prompting the user to input a number.

5. The user's input is then captured and stored in the num variable using cin. The loop condition num <=
10 is evaluated. If the entered number is less than or equal to 10, the loop continues to the next
iteration, prompting the user for input again.

6. If the condition num <= 10 is false, showing that the entered number is greater than 10, the loop is
exited. After the loop, the code displays a message using cout, indicating that the number provided by
the user is greater than 10.

7. The value of num is displayed in the output message using cout. The return 0; statement shows
successful program termination.
Functions

(Pre-defined Functions)

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

int main() {

double num;

cout << "Enter a number: ";

cin >> num;

double squareRoot = sqrt(num);

cout << "Square root of " << num << " is: " << squareRoot << endl;

double base, exponent;

cout << "Enter the base: ";

cin >> base;

cout << "Enter the exponent: ";

cin >> exponent;

double powerResult = pow(base, exponent);

cout << base << " raised to the power of " << exponent << " is: " << powerResult << endl;

string word;

cout << "Enter a word: ";

cin >> word;


int length = word.length();

cout << "Length of the word \"" << word << "\" is: " << length << endl;

string phrase;

cout << "Enter a phrase: ";

cin.ignore();

getline(cin, phrase);

string fullSentence = word + " " + phrase;

cout << "Full sentence: " << fullSentence << endl;

return 0;

Explanation of the code

1. It calculates the square root of a number specified by the user with sqrt() from the <cmath> library.

2. It calculates the result of raising a base to an exponent given by the user using pow() from <cmath>.
3. It determines the length of a word entered by the user using the length() function of the string class.

4. It concatenates a word, and a phrase entered by the user via the + operator.

5. It displays the results of the calculations and string operations.

Self-Assessment

1. What is a computer program?

Computer programs are instructions that tell a computer what to do. Programs are written in a
programming language, which is a set of rules that define how the instructions are to be
interpreted.

2. What are the three basic control structures?

The three basic control structures are sequence, selection, and iteration.

3. What is a function?

Functions are blocks of code that perform a specific task. Functions are used to break down large
programs into smaller, more manageable pieces.

4. What are the advantages of using functions?

 They make the code easier to read and understand.

 They make the code easier to reuse.

 They can improve performance by reducing the amount of code executed.

5. What are the different types of functions?

 C++ standard library provides pre-defined functions. They are used to perform common
tasks, such as input and output, string manipulation, mathematical operations, and
more.
 User-defined functions are functions that are written by the programmer.

 Library functions are functions that are provided by the programming language or a
library.
6. What are the different ways to call a function?

 By using the function name followed by a list of arguments.


 By using the function name followed by a void keyword.

7. What are the different ways to return a value from a function?

 By using the return keyword followed by a value.


 By using the return keyword without a value.
8. What are the different types of errors that can occur in a function?

 Syntax errors: These errors occur when the code does not follow the rules of the
programming language.
 Runtime errors: These errors occur when the code is executed and the computer
encounters a situation that it cannot handle.
 Logic errors: These errors occur when the code does not produce the desired results.

9. What is a sequence?

A sequence is a group of statements that are executed in order.

10. What is selection?

Selection is a control structure that allows the program to choose between different paths of
execution based on a condition.

11. What is iteration?

Iteration is a control structure that allows the program to repeat a block of statements a certain
number of times or until a condition is met.

12. What is a recursive function?

A recursive function is a function that calls itself.

13. What is a parameter?

A parameter is a variable that is passed to a function.

14. What is a return value?

A parameter is a variable that is passed to a function.

15. What is a local variable?


A local variable is a variable that is declared within a function.

16. What is a global variable?

A global variable is a variable that is declared outside of any function.

17. What is a constant?

A constant is a value that cannot be changed.

18. What is a data type?

A data type is a classification of data that tells the computer how to store and manipulate the
data.

19. What is an operator?

A data type is a classification of data that tells the computer how to store and manipulate the
data.

20. What is a statement?

A data type is a classification of data that tells the computer how to store and manipulate the
data.

You might also like