Program to find the time after K minutes from given time

You are given a time T in 24-hour format (hh:mm) and a positive integer K, you have to tell the time after K minutes in 24-hour time.

Examples:

Input: T = 12:43, K = 21
Output: 13:04

Input: T = 20:39, K = 534
Output: 05:33

Approach:

  • Convert the given time in minutes
  • Add K to it let it be equal to M.
  • Convert the M minutes in 24 hours format accordingly.
  • Below is the implementation of the above approach:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    // function to obtain the new time
    void findTime(string T, int K)
    {
      
        // convert the given time in minutes
        int minutes = ((T[0] - '0')
                           * 10
                       + T[1] - '0')
                          * 60
                      + ((T[3] - '0')
                             * 10
                         + T[4] - '0');
      
        // Add K to current minutes
        minutes += K;
      
        // Obtain the new hour
        // and new minutes from minutes
        int hour = (minutes / 60) % 24;
      
        int min = minutes % 60;
      
        // Print the hour in appropriate format
        if (hour < 10) {
            cout << 0 << hour << ":";
        }
        else {
            cout << hour << ":";
        }
      
        // Print the minute in appropriate format
        if (min < 10) {
            cout << 0 << min;
        }
        else {
            cout << min;
        }
    }
      
    // Driver code
    int main()
    {
      
        string T = "21:39";
      
        int K = 43;
      
        findTime(T, K);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program of above approach
    class GfG {
      
    // function to obtain the new time 
    static void findTime(String T, int K) 
      
        // convert the given time in minutes 
        int minutes = ((T.charAt(0) - '0'
                        * 10
                    + T.charAt(1) - '0'
                        * 60
                    + ((T.charAt(3) - '0'
                            * 10
                        + T.charAt(4) - '0'); 
      
        // Add K to current minutes 
        minutes += K; 
      
        // Obtain the new hour 
        // and new minutes from minutes 
        int hour = (minutes / 60) % 24
      
        int min = minutes % 60
      
        // Print the hour in appropriate format 
        if (hour < 10
        
            System.out.print("0" + hour + ":"); 
        
        else 
        
            System.out.print(hour + ":"); 
        
      
        // Print the minute in appropriate format 
        if (min < 10
        
            System.out.println("0" + min); 
        
        else 
        
            System.out.println(min); 
        
      
    // Driver code 
    public static void main(String[] args) 
      
        String T = "21:39"
        int K = 43
        findTime(T, K); 
    }
      
    // This code is contributed by Prerna Saini

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 program for given approach
      
    # function to obtain the new time
    def findTime(T, K):
          
        # convert the given time in minutes
        minutes = (((ord(T[0]) - ord('0'))* 10 + 
                     ord(T[1]) - ord('0'))* 60 + 
                   ((ord(T[3]) - ord('0')) * 10 + 
                     ord(T[4]) - ord('0')));
                      
        # Add K to current minutes
        minutes += K
      
        # Obtain the new hour
        # and new minutes from minutes
        hour = (int(minutes / 60)) % 24
      
        min = minutes % 60
      
        # Print the hour in appropriate format
        if (hour < 10):
            print(0, hour, ":", end = " ")
      
        else:
            print(hour, ":", end = " ")
      
        # Print the minute in appropriate format
        if (min < 10):
            print(0, min, end = " ")
      
        else:
            print(min,end = " ")
      
    # Driver code
    if __name__ == '__main__':
        T = "21:39"
      
        K = 43
      
        findTime(T, K)
          
    # This code is contributed by
    # Surendra_Gangwar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program of above approach
    using System;
      
    class GfG
    {
          
        // function to obtain the new time 
        static void findTime(string T, int K) 
        
          
            // convert the given time in minutes 
            int minutes = ((T[0] - '0'
                            * 10
                        + T[1] - '0'
                            * 60
                        + ((T[3] - '0'
                                * 10
                            + T[4] - '0'); 
          
            // Add K to current minutes 
            minutes += K; 
          
            // Obtain the new hour 
            // and new minutes from minutes 
            int hour = (minutes / 60) % 24; 
          
            int min = minutes % 60; 
          
            // Print the hour in appropriate format 
            if (hour < 10) 
            
                Console.Write("0" + hour + ":"); 
            
            else
            
                Console.Write(hour + ":"); 
            
          
            // Print the minute in appropriate format 
            if (min < 10) 
            
                Console.Write("0" + min); 
            
            else
            
                Console.Write(min); 
            
        
        // function to obtain the new time 
          
        // Driver code 
        public static void Main() 
        
          
            string T = "21:39"
            int K = 43; 
            findTime(T, K); 
        
    }
      
    // This code is contributed by ihritik

    chevron_right

    
    

    PHP

    Output:

    22:22
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.