Tuesday, January 22, 2019

Add One Second

Write a program that adds one second to a clock time, given its hours, minutes and seconds.

Input : Input consists of three natural numbers h, m and s that represent a clock time, that is, such that h<24, m<60 and s<60.

Output : Print the new clock time defined by h, m and s plus one second in the format “HH:MM:SS”.

#include <iostream>
using namespace std;

int main () { 
    int h, m, s;
    cin >> h >> m >> s;
    
    if (s==59) { m = m+1, s=0;
    }
    else s = s+1;
    
    if (m==60) { m=0, h=h+1;
    }
    
    if (h==24) h=0;
    
    if (h<10) cout << "0" << h << ":";
    else cout << h << ":";
    
    if (m<10) cout << "0" << m << ":";
    else cout << m << ":";
    
    if (s<10) cout << "0" << s << endl;
    else cout << s << endl;
    
    
}

No comments:

Post a Comment

Modelo OSI

Open System Interconnection , Interconexión de Sistemas Abiertos, es un modelo para estudiar las categorías en que se pueden dividir los pr...