Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to input time in millisecond s and show in minutes and seconds

C Code Example — Basic Programs

ADVERTISEMENT

C Program to input time in millisecond s and show in minutes and seconds

Objective

Write a C program to convert time in milliseconds to minutes and seconds.

Algorithm / Approach

  1. Declare integers tms, tm, and ts.
  2. Read the total milliseconds from the user.
  3. Convert to total seconds: ts = tms / 1000.
  4. Calculate the minutes: tm = ts / 60.
  5. Calculate the leftover seconds: ts = ts % 60.
  6. Print the result.
main.c
#include<stdio.h>
int main( ) {
 int tms, tm, ts;
 printf("Enter Time in millis : ");
 scanf("%d",&tms);
 ts = tms/1000;
 tm = ts/60;
 ts = ts%60;
 printf("TIME(mm:ss) - %d:%d",tm,ts);
 return 0;
}

Expected Output

Enter Time in millis:863534
TIME(mm:ss) - 14:39

Explanation of the Program

  • This program uses both integer division and the modulo operator.
  • Integer division (/) is used to discard remainder data (e.g., finding how many full minutes fit into the total seconds).
  • The Modulo operator (%) does the exact opposite: it discards the quotient and returns ONLY the remainder. ts % 60 gives us the leftover seconds that didn't perfectly fit into a full minute.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT