Multiplying a fraction of a number to a whole number

I am working on this basic project that is fairly easy
when I do the math. But I can not figure out the code.

for example I want to convert 50931 seconds to hrs, minutes and seconds.

50391 / 3600 = 13.9975 Hrs.

Now I want to multiply .9975 by 60.

How do you code so that only fraction part of a number is multiplied
by another whole number? .9975 * 60


.9975 * 60= 59.85 Min
.85 * 60 = 51 Sec

Thanks.

Shervin
Last edited on
You don't want to use (floating point) fractions. Study this:
1
2
3
4
int x = 50391;
const int divider = 60;
const int seconds = x % divider; // modulus operator
const int y = x / divider; // integer division discards fraction 

closed account (jyU4izwU)
is this your answer?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream.h>
#include <conio.h>

int main(void)
{
int com001,com002,com003,com004,com005,com006,com007,com008,com009;
int sec,min,h,total;
int end,start,ex;
clrscr();
cout << "\n Please Chose a Command (Type List For List Of Commands);
cin >> com001;
  if (com001 == List )
    {
//------------------------------------------------------------------------
      cout << "\n Command Name:SM Command Is:Sec To Min";
      cout << "\n Command Name:MS Command Is:Min To Sec";
      cout << "\n Command Name:SH Command Is:Sec To H"   ;
      cout << "\n Command Name:HS Command Is:H To Sec"   ;
      cout << "\n Command Name:MH Command Is:Min To H"   ;
      cout << "\n Command Name:HM Command Is:H To Min"   ;
      cin >> end;
//------------------------------------------------------------------------


  else if (com001 == SM )
    {
      cout << "Please Enter Your Seconds: ";
      cin >> sec;
      sec*60=total;
      cout << "Your Total is " << total;
      cin >> end;
    }


      else if (com001 == MS )
    {
      cout << "Please Enter Your Minutes: ";
      cin >> min;
      min/60=total;
      cout << "Your Total is " << total;
      cin >> end;
    }


          else if (com001 == SH )
// you know the rest...
} 
The sample project already gives the number of seconds = 50931
So if I divide that by 3600 seconds per hour i will get 13.99975 Hrs.
Now All I need to know is that how to eliminate the 13 and only multiply the
.99975 by 60 to get 59.85 minutes and again .85 multiply be 60 to get 51 seconds.
It is the first chapter of the book and is not suppose to be complicated.

Thanks
Sghamghamy, I really puzzled over this type of problem myself for a good while!

Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

main()
{
int seconds, mins;

cout<<"Enter the number of seconds: " ;
cin>> seconds;
cin.get();

cout<<"\nThis is equal to: "<< seconds/3600<< " hours "
    <<seconds%3600/60<< " minutes"
    <<" and " << (seconds%3600/60.0 - seconds%3600/60)*60 <<" seconds.";
cin.get ();

return 0;
}


Even though this looks pretty scarey, it's not! The answer, as Keskiverto says above, lies in using modulus division and integer division.

Modulus division, where we use % to divide a number, returns only the remainder, and not the whole number.

Integer division, on the other hand, returns only the whole number and discards the remainder. Two extremely useful things to know!

The interesting exception to this is that by adding .0 to an integer when dividing, you will indeed get a decimal fraction if there should be one.

You will find these three methods used in the code above.

There are plenty of ways to handle your problem, some a lot more elegant, but this is the only way I know at present.

Use a lot of simple examples till you get the hang of it. Good luck! Don
That line 14 ... seconds%60 ...
hey,
you could use string data type to store a decimal number.
then, you could retrieve the number after the decimal point(as the point would be an element of the string and can be referenced using an array index).
multiply this retrieved number by 60, store it in a string again. and finally insert the decimal point into it based on the no. of places in the original no(the no of places in .9975 is 4, for ex). on multiplying 9975 by 60 you would get 598500, on placing the point appropriately you end up with 59.85!@
neelakshi wrote:
you could use string data type to store a decimal number.
then, you could retrieve the number after the decimal point(as the point would be an element of the string and can be referenced using an array index).
"How to make elementary operations slow even on modern computers"

Use either y = modf(x, &z) or y = x - trunc(x); if you want fraction .
http://en.cppreference.com/w/cpp/numeric/math/modf
But solutions using / and % is better in your case.
Hi Donnie,
I tried your code and it is working perfectly. However I am a little confused about line 14.

Why 60.0. ? and why are you multiplying the result of subtraction by 60.?


Kindly explain. Thanks.

Shervin
Hi Shervin,

The very first part of line 14, which is:
(seconds%3600
is a modulus division, which ignores total hours and only produces the remainder in seconds left over. (Which in your case is 3591 seconds.)

As you can see from this, modulus division returns a remainder, not a decimal fraction. (A very important difference!)

Then you bring these seconds to minutes along with the decimal part by dividing by 60.0. You can do all this in one expression. (If you divided by just plain 60 you would just get whole minutes, because that is what you normally get with integer division. Remember, seconds and mins were declared as integers, not as floats or doubles, which are normally used for decimal calculations.)

However, this is exactly what is needed for the second expression in line 14 -
seconds%3600/60
- where we just want whole minutes! Neat trick!

So on your original figure of 50931 seconds, we now have 13 hours (from line12) with 3591 seconds left over.

Hence line 14 gives us: (59.85 mins - 59 mins)*60 to bring it up to seconds.

Can't stress too much the need to check out each individual step in a mini program until you get the hang of it.

Good luck! Donnie


(59.85 mins - 59 mins)*60sec

or

50931sec % 60  == C sec
50931sec / 60  == x min
x min % 60     == B min
x min / 60     == A h

==> A h, B min, C sec
Thank you both. I understand it perfectly now.
Shervin
Topic archived. No new replies allowed.