Trying to convert hours to minutes in C++
Trying to convert hours to minutes in C++
Apparently this is pretty basic but I cant figure it out for the life of me!
I am trying to change hours such as 1230 (12:30) into minutes.
I think I need to divide 1230 by 100 and store: (answer*60) + remainder
How do I do this? I think I should use the modulus operator but I am not sure how to do it. Any help would be welcome. Thanks!
20 Sep 06, 6:45AM
ok, your single 4 digit number actually has the correct number of minutes already stated (30 in you example), however the hours need multiplying by 60.
When you divide by 100, you end up with a number, say 12.3, which is has the wrong fraction, i.e. its really should be 12.5, i.e. 12 and half hours. Therefore we need to correct this. You therefore want to calculate:
or:
double t = time/100.0; int minutes = (t-int(t))*100 + int(t)*60 return
25 Feb 07, 9:40PM
{ int hrs, mins, rem, x; //input number of hours x=hrs*60;//multiply hours by 60 rem=mins+x;//add the number of minutes cout<<"the time is<<rem<<"minutes"; }
we just used borland version of C++...the algorithm is actually a number of hours and minutes...e.g. 12:30...first 12(the number of hours) multiply by 60 then add the number of minutes such as 30...the result shouldbe 750...
in this case it is much simplier for you...just try it27 Jul 11, 7:33AM
Hi..This is the coding for convert minutes into hours,
#include <iostream> using namespace std; int Convert(int); int remainder; int main () { int minutes; int convert_to_hours; int remainder; cout << "This program converts minutes to hours and minutes. " << endl; cout << "Please enter a number of minutes (an integer) to be converted:" << endl; cin >> minutes; convert_to_hours = Convert(minutes); if (convert_to_hours > 1) cout << minutes << " mintes equal to " << convert_to_hours << " hours "; else cout << minutes << " mintes equal to " << convert_to_hours << " hour "; if (remainder > 1) cout << "and " << remainder << "minutes." << endl; else cout << "and " << remainder << "minute." << endl; return 0; } int Convert(int minutes) { int convert_to_hours; convert_to_hours = minutes / 60; return convert_to_hours; } int Convert(int remainder) { int remainder; remainder = minutes % 60; return remainder; }
Login