Home > Software design >  What does modulus do in this line?
What does modulus do in this line?

Time:09-20

I just need to understand why does minutes` works. this code is supposed to get minutes input and tell you how many days,hours,minutes can fit into it (there are 1440 minutes in a day).

     using System;

 public class Time
 {
     public static void Main()
     {
         int minutes = int.Parse(Console.ReadLine());
         int days = (minutes/1440);
         int hours = (minutes40)/60;
         Console.WriteLine("days: "   days   " hours: "   hours   " minutes: "   minutes`);

     }
 }

CodePudding user response:

% is the remainder operator: https://en.wikipedia.org/wiki/Modulo_operation

minutes % 60

when that snippet is evaluated it will remove all extra minutes, leaving only the remaining minutes that total less than 1 hour.

CodePudding user response:

Think of y = minutes40 this way

int x = (minutes/1440);
int y = minutes - 1440*x

The value of y holds the remainder of the division minutes/1440

  • Related