# Add given n time durations

Given n time durations in the form of MM : SS, where MM denotes minutes and SS denotes the seconds. The task is to add all the time duration and output answer in the form of HH : MM : SS.

Examples:

```Input : n = 5
duration1 = 0 : 45
duration1 = 2 : 31
duration1 = 3 : 11
duration1 = 2 : 27
duration1 = 1 : 28
Output : 0 : 10 : 22
Initially, sum = 0
On adding duration 1, sum = 0 hour 0 minutes 45 seconds.
On adding duration 2, sum = 0 hour 3 minutes 16 seconds.
On adding duration 3, sum = 0 hour 6 minutes 27 seconds.
On adding duration 4, sum = 0 hour 8 minutes 54 seconds.
On adding duration 5, sum = 0 hour 10 minutes 22 seconds
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to convert all the given time duration to seconds. Once we know the duration in seconds, we can calculate the sum of durations in seconds.
In order to get the number of hours, we have to divide the total duration in seconds by 3600. The remainder is used to calculate the number of minutes and seconds. By dividing the remainder with 60 we get the number of minutes, and the remainder of that division is the number of seconds.

Below is the implementation of this approach:

## C++

 `// CPP Program to find the sum of all duration  ` `// in the form of MM : SS. ` `#include ` `using` `namespace` `std; ` ` `  `// Print sum of all duration. ` `void` `printSum(``int` `m[], ``int` `s[], ``int` `n) ` `{ ` `    ``int` `total = 0; ` ` `  `    ``// finding total seconds ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``total += s[i]; ` `        ``total += (m[i] * 60); ` `    ``} ` ` `  `    ``// print the hours. ` `    ``cout << total / 3600 << ``" : "``; ` `    ``total %= 3600; ` ` `  `    ``// print the minutes. ` `    ``cout << total / 60 << ``": "``; ` `    ``total %= 60; ` ` `  `    ``// print the hours. ` `    ``cout << total << endl; ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `m[] = { 0, 2, 3, 2, 1 }; ` `    ``int` `s[] = { 45, 31, 11, 27, 28 }; ` `    ``int` `n = ``sizeof``(m)/``sizeof``(m); ` `    ``printSum(m, s, n); ` `    ``return` `0; ` `} `

## Java

 `// Java Program to find the ` `// sum of all duration in ` `// the form of MM : SS. ` `class` `GFG ` `{ ` `     `  `    ``// Print sum of all duration. ` `    ``static` `void` `printSum(``int` `m[],  ` `                    ``int` `s[], ``int` `n) ` `    ``{ ` `        ``int` `total = ``0``; ` `     `  `        ``// finding total seconds ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``total += s[i]; ` `            ``total += (m[i] * ``60``); ` `        ``} ` `     `  `        ``// print the hours. ` `        ``System.out.print(total / ``3600` `+ ``" : "``); ` `        ``total %= ``3600``; ` `     `  `        ``// print the minutes. ` `        ``System.out.print(total / ``60` `+``": "``); ` `        ``total %= ``60``; ` `     `  `        ``// print the hours. ` `        ``System.out.println(total); ` `    ``}  ` `     `  `// Driver code  ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `m[] = {``0``, ``2``, ``3``, ``2``, ``1` `}; ` `    ``int` `s[] = { ``45``, ``31``, ``11``, ``27``, ``28` `}; ` `    ``int` `n = m.length; ` `     `  `    ``printSum(m, s, n); ` `} ` ` `  `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 code to find the sum of ` `# all duration in the form of MM : SS. ` ` `  `# Print sum of all duration. ` `def` `printSum (m, s, n ): ` `    ``total ``=` `0` `     `  `    ``# finding total seconds ` `    ``for` `i ``in` `range``(n): ` `        ``total ``+``=` `s[i] ` `        ``total ``+``=` `(m[i] ``*` `60``) ` `     `  `    ``# print the hours. ` `    ``print``(``int``(total ``/` `3600``) , end``=` `" : "``) ` `    ``total ``%``=` `3600` `     `  `    ``# print the minutes. ` `    ``print``(``int``(total ``/` `60``) , end``=``": "``) ` `    ``total ``%``=` `60` `     `  `    ``# print the hours. ` `    ``print``(``int``(total)) ` ` `  `# Driven Code ` `m ``=` `[ ``0``, ``2``, ``3``, ``2``, ``1` `] ` `s ``=` `[ ``45``, ``31``, ``11``, ``27``, ``28` `] ` `n ``=` `len``(m) ` `printSum(m, s, n) ` ` `  `# This code is contributed by "Sharad_Bhardwaj". `

## C#

 `// C# Program to find the ` `// sum of all duration in ` `// the form of MM : SS. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Print sum of all duration. ` `    ``static` `void` `printSum(``int` `[]m,  ` `                    ``int` `[]s, ``int` `n) ` `    ``{ ` `        ``int` `total = 0; ` `     `  `        ``// finding total seconds ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``total += s[i]; ` `            ``total += (m[i] * 60); ` `        ``} ` `     `  `        ``// print the hours. ` `        ``Console.Write(total / 3600 + ``" : "``); ` `        ``total %= 3600; ` `     `  `        ``// print the minutes. ` `        ``Console.Write(total / 60 +``": "``); ` `        ``total %= 60; ` `     `  `        ``// print the hours. ` `        ``Console.Write(total); ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]m = {0, 2, 3, 2, 1 }; ` `        ``int` `[]s = { 45, 31, 11, 27, 28 }; ` `        ``int` `n = m.Length; ` `         `  `        ``printSum(m, s, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```0 : 10: 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.

Improved By : vt_m, Akanksha_Rai

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.