Write a C++ program to read integers with following significance. The first inte
ID: 3627613 • Letter: W
Question
Write a C++ program to read integers with following significance.The first integer value represents a time of day on a 24 hour clock, so that 1245 represents quarter to one mid-day, for example.
The second integer represents a time duration in a similar way, so that 345 represents three hours and 45 minutes.
This duration is to be added to the first time, and the result printed out in the same notation, in this case 1630 which is the time 3 hours and 45 minutes after 12.45.
Typical output might be. There are a few extra marks for spotting.
Start time is 1415. Start time is 2300.
Duration is 50. Duration is 200.
End time is 1505. End time is 100.
Explanation / Answer
let me know if you need alternations#include<iostream> using namespace std; int checkTime(int time) { int min = time % 100; //gets the minutes int hour = time / 100; //gets the hours if(min >= 60) //checks if min is > 60 if so adds an hour and subtracts the minutes { min -= 60; hour++; } hour %= 24; //mods the hour by 24 incase it's 24+ time = (hour * 100) + min; //corrects the time format return time; } void printTime(int time) { if(time < 60) { cout << "00"; //checks if its like 30 which should be 0030 if(time < 10) cout << "0"; //incase it's like 9 which should be 09 } else if((time / 100) < 10) cout << "0"; //checks if its like 130 which should be 0130 else if((time / 100) == 0) cout << "00"; //checks if its like 23 which should be 0023 cout << time; cout << endl; } int main() { int time, duration; cout << "Input the time in 24 hour format: "; cin >> time; cout << "Input the duration in minutes: "; cin >> duration; time = checkTime(time); cout << "Start time: "; printTime(time); cout << "Duration: "; printTime(duration); time = checkTime(time + duration); cout << "End time: "; printTime(time); system("pause"); return 0; } let me know if you need alternations
#include<iostream> using namespace std; int checkTime(int time) { int min = time % 100; //gets the minutes int hour = time / 100; //gets the hours if(min >= 60) //checks if min is > 60 if so adds an hour and subtracts the minutes { min -= 60; hour++; } hour %= 24; //mods the hour by 24 incase it's 24+ time = (hour * 100) + min; //corrects the time format return time; } void printTime(int time) { if(time < 60) { cout << "00"; //checks if its like 30 which should be 0030 if(time < 10) cout << "0"; //incase it's like 9 which should be 09 } else if((time / 100) < 10) cout << "0"; //checks if its like 130 which should be 0130 else if((time / 100) == 0) cout << "00"; //checks if its like 23 which should be 0023 cout << time; cout << endl; } int main() { int time, duration; cout << "Input the time in 24 hour format: "; cin >> time; cout << "Input the duration in minutes: "; cin >> duration; time = checkTime(time); cout << "Start time: "; printTime(time); cout << "Duration: "; printTime(duration); time = checkTime(time + duration); cout << "End time: "; printTime(time); system("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.