LearnFrenzy
Sign in Take a free test

Operators and Expressions · Operator Programs · Question 6

Clock time after adding minutes using % in C Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

Find the time on a 24-hour clock 1000 minutes after 22:30, and 45 minutes before 00:10.

Solution
main.c
#include <stdio.h>

void add_minutes(int h, int m, int delta) {
    int total = h * 60 + m + delta;
    total = ((total % 1440) + 1440) % 1440;       /* wrap around midnight, also for negative delta */
    printf("%02d:%02d %+d min -> %02d:%02d\n", h, m, delta, total / 60, total % 60);
}

int main(void) {
    add_minutes(22, 30, 1000);
    add_minutes(0, 10, -45);
    return 0;
}

Output Verified by compiling and running

22:30 +1000 min -> 15:10
00:10 -45 min -> 23:25

A day has 1440 minutes, so the clock is arithmetic modulo 1440. Adding 1440 before the second % fixes C's negative remainders: −35 becomes 1405 minutes = 23:25.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links