Group
Functions in C++
Objective
1. Define a function that takes an integer input representing total minutes.
2. Use integer division to calculate the number of hours.
3. Use the modulus operator to calculate the remaining minutes.
4. In the main function, ask the user for input and display the result using the conversion function.
Write a function that converts minutes to hours and minutes.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Function to convert minutes into hours and minutes
void convertToHoursAndMinutes(int totalMinutes) {
int hours = totalMinutes / 60; // Calculate how many full hours
int minutes = totalMinutes % 60; // Calculate the remaining minutes
// Print the result
cout << totalMinutes << " minutes is equal to " << hours << " hour(s) and " << minutes << " minute(s)." << endl;
}
// Main function
int main() {
int inputMinutes;
// Prompt the user to enter the number of minutes
cout << "Enter the number of minutes: ";
cin >> inputMinutes;
// Call the function to perform the conversion
convertToHoursAndMinutes(inputMinutes);
return 0; // End of the program
}
Output
Enter the number of minutes: 130
130 minutes is equal to 2 hour(s) and 10 minute(s).