CCS0007- Laboratory Exercise 1
CCS0007- Laboratory Exercise 1
CCS007L
(COMPUTER PROGRAMMING 2)
EXERCISE
1
User Defined Functions and Parameters
Section:
BSITWMA-TW01
Professor:
John Benedic Enriquez
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
Analyze a complex problem and identify and define the computing requirements
appropriate to its solution. [PO: B]
Functions:
Procedure, subprograms, and method
A function may return a value (produce a value) or may perform some action without
returning a value.
Functions that do not return a value are called void functions.
int main() {
int choice;
int number1, number2, ans;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 10); // Set text color to green (10 is the color code for green)
do {
cout << "\n--------------------------------------------------------------------------\n"
<< " WELCOME TO MATHEMATICS CALCULATOR APPLICATION OPERATORS! \n"
<< "--------------------------------------------------------------------------\n";
switch (choice)
{
//Addition Operator
case 1:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 + number2;
cout << "The sum of the addition of two numbers are: " << ans << endl;
break;
//Multiplication Operator
case 3:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 * number2;
cout << "The product of the mulitplication of two numbers are: " << ans << endl;
break;
//Division Operator
case 4:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 / number2;
cout << "The quotient of the division of two numbers are: " << ans << endl;
break;
//Modulos Operator
case 5:
cout << "Input two numbers: \n";
cin >> number1 >> number2;
ans = number1 % number2;
cout << "The remainder of the modulos of two numbers are: " << ans << endl;
break;
default:
SetConsoleTextAttribute(h, 12); // Set text color to red
cout << "Invalid choice. Please try again.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
break; // Break out of the switch case
}
} while (choice < 1 || choice > 6); // Ensures the user enters a valid choice (1-6)
char continueChoice;
if (continueChoice == 'Y') {
main(); // Restart the program
}
else if (continueChoice == 'N') {
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
break;
}
else
{
SetConsoleTextAttribute(h, 12); // Set text color to red (12 is the color code for red)
cout << "Invalid input. Please enter 'Y' or 'N' only.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
}
}
return 0;
}
/*Formative1*/
//Deadline 01-31-25
/*CCS0007L*/
int main() {
int choice;
int number1, number2, ans;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 10); // Set text color to green (10 is the color code for green)
do {
cout << "\n--------------------------------------------------------------------------\n"
<< " WELCOME TO AREA OF POLYGONS CALCULATOR APPLICATION! \n"
<< "--------------------------------------------------------------------------\n";
switch (choice)
{
//Area of Square
case 1:
int number1; int squared;
cout << "\nEnther the side of the Square:\n";
cin >> number1;
cout << "\n===========================";
cout << "\n AREA OF SQUARE ";
cout << "\n===========================";
squared = number1 * number1;
cout << "\nThe area is " << " " << squared << " " << "sq. units" << endl;
break;
//Area of Rectangle
case 2:
int rectangle, length, width1;
cout << "\nEnther the length and width of the Rectangle:\n";
cin >> length >> width1;
cout << "\n===========================";
cout << "\n AREA OF RECTANGLE ";
cout << "\n===========================";
rectangle = length * width1;
cout << "\nThe area is " << " " << rectangle << " " << "sq. units" << endl;
break;
//Area of Triangle
case 3:
CCS007L-Computer Programming 2 Page 11 of
14
int base, height;
cout << "\nEnther the length and width of the Rectangle:\n";
cin >> base >> height;
cout << "\n===========================";
cout << "\n AREA OF TRIANGLE ";
cout << "\n===========================";
cout << "\nThe area is " << (base * height) / 2 << " sq. units" << endl;
break;
//Area of Circle
case 4:
int radius; float circle;
cout << "\nEnther the radius of the Circle:\n";
cin >> radius;
circle = 3.14159265359 * radius * radius;
cout << "\n===========================";
cout << "\n AREA OF CIRCLE ";
cout << "\n===========================";
cout << "\nThe area is " << " " << circle << " " << "sq. units" << endl;
break;
default:
SetConsoleTextAttribute(h, 12); // Set text color to red
cout << "Invalid choice. Please try again.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
break;
}
} while (choice < 1 || choice > 5); // Restricts the user on choosing only from 1-5
char continueChoice;
while (true) {
cout << "\nDo you want to continue? (Y/N): ";
cin >> continueChoice;
continueChoice = toupper(continueChoice); // Convert to uppercase for consistency
if (continueChoice == 'Y') {
main(); // Restart the program
}
else if (continueChoice == 'N') {
cout << "\n************************************************************\n"
<< " THANK YOU FOR USING THE APPLICATION! \n"
CCS007L-Computer Programming 2 Page 12 of
14
<< " HAVE A NICE DAY AHEAD! \n"
<< "************************************************************\n";
break;
}
else
{
SetConsoleTextAttribute(h, 12); // Set text color to red (12 is the color code for red)
cout << "Invalid input. Please enter 'Y' or 'N' only.\n";
SetConsoleTextAttribute(h, 10); // Reset text color to green
}
}
return 0;
}
/*Formative1*/
//Deadline 01-31-25
/*CCS0007L*/
Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.
What is the difference between predefined function and user defined function?
Predefined functions are used in conducting common operations such as mathematical
computation like sqrt. Another predefined function is cin and cout input and output. User
defined function on the other hand is created by the programmer/developer using their own
terms to perform a specific task.
VIII. REFERENCES
Zak, Dianne (2016). An Introduction to Programming with C++
Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
https://www.cprogramming.com/tutorial/lesson4.html