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
- Declare integers
tms,tm, andts. - Read the total milliseconds from the user.
- Convert to total seconds:
ts = tms / 1000. - Calculate the minutes:
tm = ts / 60. - Calculate the leftover seconds:
ts = ts % 60. - 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 % 60gives us the leftover seconds that didn't perfectly fit into a full minute.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)