COPROG
COPROG
Programming Fundamentals
(Logical Operators)
using namespace std;
int main() {
int num;
} else {
cout << "The number is not between 10 and 20." << endl;
cout << "The number is less than 0 or greater than 100." << endl;
} else {
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>
int main() {
} else {
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. 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>
int main() {
if (num1 == num2) {
} else {
if (num1 != num2) {
} else {
cout << "The numbers are equal." << endl;
cout << num1 << " is greater than " << num2 << endl;
} else {
cout << num1 << " is not greater than " << num2 << endl;
cout << num1 << " is less than " << num2 << endl;
} else {
cout << num1 << " is not less than " << num2 << endl;
cout << num1 << " is greater than or equal to " << num2 << endl;
} else {
cout << num1 << " is not greater than or equal to " << num2 << endl;
cout << num1 << " is less than or equal to " << num2 << endl;
} else {
cout << num1 << " is not less than or equal to " << num2 << endl;
}
return 0;
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.
Control Structures
(while loop)
#include <iostream>
int main() {
int count = 1;
while (count <= 5) {
count++;
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 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>
int main() {
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
#include <iostream>
int main() {
int num;
do {
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>
int main() {
double num;
cout << "Square root of " << num << " is: " << squareRoot << endl;
cout << base << " raised to the power of " << exponent << " is: " << powerResult << endl;
string word;
cout << "Length of the word \"" << word << "\" is: " << length << endl;
string phrase;
cin.ignore();
getline(cin, phrase);
return 0;
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.
Self-Assessment
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.
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.
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?
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?
Selection is a control structure that allows the program to choose between different paths of
execution based on a condition.
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.
A data type is a classification of data that tells the computer how to store and manipulate the
data.
A data type is a classification of data that tells the computer how to store and manipulate the
data.
A data type is a classification of data that tells the computer how to store and manipulate the
data.