Program to find the time after K minutes from given time Last Updated : 13 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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 minutesAdd K to it let it be equal to M.Convert the M minutes in 24 hours format accordingly. C++ #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); } Java // 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 Python3 # 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 C# // 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 PHP <?php // PHP program of above approach // function to obtain the new time function findTime($T, $K) { // convert the given time in minutes $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 $hour = (int)($minutes / 60) % 24; $min = $minutes % 60; // Print the hour in appropriate format if ($hour < 10) { echo 0 . $hour . ":"; } else { echo $hour . ":"; } // Print the minute in appropriate format if ($min < 10) { echo 0 . $min; } else { echo $min; } } // Driver code $T = "21:39"; $K = 43; findTime($T, $K); // This code is contributed by Akanksha Rai ?> JavaScript <script> // Javascript program of above approach // function to obtain the new time function findTime(T, K) { // convert the given time in minutes var minutes = ((Number(T[0]) - '0') * 10 + Number(T[1]) - '0') * 60 + (( Number(T[3]) - '0') * 10 + Number(T[4]) - '0') ; // Add K to current minutes minutes += K; // Obtain the new hour // and new minutes from minutes var hour = (minutes / 60) % 24; var min = minutes % 60; // Print the hour in appropriate format if (hour < 10) { document.write("0" + hour + ":"); } else { document.write(hour.toFixed() + ":"); } // Print the minute in appropriate format if (min < 10) { document.write("0" + min); } else { document.write(min); } } // function to obtain the new time // Driver code var T = "21:39"; var K = 43; findTime(T, K); </script> Output: 22:22 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to find the time after K minutes from given time N NaimishSingh Follow Improve Article Tags : DSA Similar Reads Find the time which is palindromic and comes after the given time Given a string str which stores the time in the 24 hours format as HH:MM such that 0 ? HH ? 23 and 0 ? MM ? 59. The task is to find the next closest time which is a palindrome when read as a string. If no such string exists then print -1.Examples: Input: str = "21:12" Output: 22:22 The only palindro 10 min read Time Difference between given times in HH:MM:SS format Given 2 times 'st' and 'et' in HH: MM: SS format. The task is to print the time difference between st and et in HH:MM: SS format Examples: Input: st = 13:50:45, et = 14:55:50Output: 01:05:05Explanation: The time gap is 1 hour 5 minutes and 5 seconds. Input: st = 12:00:00, et = 24:00:00Output: 12:00: 6 min read Program to find the time remaining for the day to complete Given the current time in the form of HH::MM, where H represents the hours and M, represents the minutes in a 24-hour time format. The task is to calculate the time remaining for the day to complete as HH::MM.Examples: Input: HH::MM = 00::01 Output: 23::01 Input: HH::MM = 23::55 Output: 00::05 Appro 3 min read Minimum minutes needed to make the time palindromic Given string str which stores the time in the 24 hours format as "HH: MM". The task is to find the minimum minutes that need to be added to make time palindromic. Examples: Input: str = "05:39" Output: 11 Explanation: It takes 11 minutes for minute value to become 50, 05:50 is a palindromic time Exa 6 min read Program to convert time from 12 hour to 24 hour format Given a time at 12-hour AM/PM format, convert it to military (24-hour) time. Note: Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clockExamples:Input: 07:05:45 PMOutput: 19:05:45Explanation: On converting the g 6 min read Like