Thread: time converting

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    time converting

    Hi! I am learning programming in C and I am trying to make a programme to convert the seconds in days, hours, minutes, and remaing seconds.
    I have been discussed this problem earlier and got good response but I'm still unable to find the desired result.

    I am using code like this

    int s,m,h,d;
    printf("Enter the number of seconds and press return:");
    scanf("%d", &s);
    // calculating the number of days
    d=(((s/60)/60)/24);
    printf("total days are %d",d);

    Now I want to calculate the remaining seconds in hours, minutes, and seconds.
    Can any one help me.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    itsme@dreams:~/C$ cat timebrkdwn.c
    #include <stdio.h>
    
    #define SEC_PER_MIN  60
    #define SEC_PER_HOUR 3600
    #define SEC_PER_DAY  86400
    
    int main(void)
    {
      int s, m, h, d;
    
      printf("Enter the number of seconds and press return: ");
      fflush(stdout);
      scanf("%d", &s);
    
      // Days
      d = s / SEC_PER_DAY;
      s %= SEC_PER_DAY;
    
      // Hours
      h = s / SEC_PER_HOUR;
      s %= SEC_PER_HOUR;
    
      // Minutes
      m = s / SEC_PER_MIN;
      s %= SEC_PER_MIN;
    
      printf("Days: %d, Hours: %d, Minutes: %d, Seconds: %d\n", d, h, m, s);
      return 0;
    }
    Code:
    itsme@dreams:~/C$ ./timebrkdwn
    Enter the number of seconds and press return: 923874
    Days: 10, Hours: 16, Minutes: 37, Seconds: 54
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM