Converts seconds to minutes and seconds with Modulus Operator: % - C Language Basics

C examples for Language Basics:printf

Description

Converts seconds to minutes and seconds with Modulus Operator: %

Demo Code

#include <stdio.h>

#define SEC_PER_MIN 60            // seconds in a minute

int main(void){
    int sec, min, left;
    //  w  ww .j a v  a2s.c  o  m
    printf("Enter the number of seconds (<=0 to quit):\n");
    scanf("%d", &sec);            
    while (sec > 0)
    {
        min = sec / SEC_PER_MIN;  // truncated number of minutes
        left = sec % SEC_PER_MIN; // number of seconds left over
        printf("%d seconds is %d minutes, %d seconds.\n", sec, min, left);
        printf("Enter next value (<=0 to quit):\n");
        scanf("%d", &sec);
    }
    printf("Done!\n");
    
    return 0;
}

Result


Related Tutorials