Coding Examples
Coding Examples
Here's the JavaScript function `courseReg` that gathers basic information from a student,
registers courses, and displays the data:
function courseReg() {
// Gather Basic Information
let studentName = prompt("Enter your name:");
let studentNumber = prompt("Enter your student number:");
let courseCount = parseInt(prompt("How many courses do you want to register for? (Up
to 5)"));
// Register Courses
let courses = [];
for (let i = 0; i < courseCount; i++) {
let courseCode = prompt(`Enter course code for course ${i + 1}:`);
courses.push(courseCode);
}
// Display Information
document.getElementById("studentname").textContent = studentName;
document.getElementById("studentnumber").textContent = studentNumber;
<h1>Course Registration</h1>
<p><strong>Student Name:</strong> <span id="studentname"></span></p>
<p><strong>Student Number:</strong> <span id="studentnumber"></span></p>
<p><strong>Registered Courses:</strong></p>
<ul id="courselist"></ul>
<button onclick="courseReg()">Register for Courses</button>
Here's a C++ program that uses function overloading and templates to calculate the
perimeter of a rectangle or square:
#include <iostream>
using namespace std;
int main() {
cout << "Perimeter of square with side 5: " << calculatePerimeter(5) << endl;
cout << "Perimeter of rectangle with length 5 and width 3: " << calculatePerimeter(5, 3)
<< endl;
cout << "Perimeter of rectangle with length 5.5 and width 3.2: " <<
calculatePerimeter(5.5, 3.2) << endl;
return 0;
}
Q1: C++ Program for Overloaded Function `display`
Here's a C++ program that includes overloaded functions for displaying different numerical
data types:
#include <iostream>
using namespace std;
int main() {
int intNum = 5;
double doubleNum = 3.14;
return 0;
}