Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Write a C Program to Convert a Given Integer (in Seconds) to Hours, Minutes and Seconds

write a C program that can convert any integer (in seconds) to hours, minutes and seconds using a while loop and a series of if-else statements.

If you are learning C programming, you might have encountered some problems that require you to convert a given integer (in seconds) to hours, minutes and seconds. For example, you might want to find out how long a video or a podcast lasts in a more human-readable format. In this program, I will show you how to write a C program that can convert any integer (in seconds) to hours, minutes and seconds.

Conversion Between Seconds and Hours, Minutes and Seconds

The conversion between seconds and hours, minutes and seconds is based on the fact that one hour equals 3600 seconds, one minute equals 60 seconds, and one second equals 1 second. Therefore, to convert a given number of seconds to hours, minutes and seconds, we need to divide the number by 3600, 60 and 1 respectively and get the quotients and remainders.

For example, if we have 5000 seconds, we can convert it to hours, minutes and seconds as follows:

  • Divide 5000 by 3600 and get the quotient and remainder: 5000 / 3600 = 1 with remainder 1400. This means that 5000 seconds equals 1 hour and 1400 seconds.
  • Divide the remaining 1400 seconds by 60 and get the quotient and remainder: 1400 / 60 = 23 with remainder 20. This means that 1400 seconds equals 23 minutes and 20 seconds.
  • Divide the remaining 20 seconds by 1 and get the quotient and remainder: 20 / 1 = 20 with remainder 0. This means that 20 seconds equals 20 seconds and nothing more.
  • Combine the quotients to get the final result: 5000 seconds = 1 hour, 23 minutes and 20 seconds.

C Program 

To write a C program that can convert a given integer (in seconds) to hours, minutes and seconds, we need to do three things:

  • Declare a function that takes one parameter (the number of seconds) and prints the hours, minutes and seconds.
  • Define the function using a while loop and a series of if-else statements to divide the number of seconds by 3600, 60 and 1 respectively and store the quotients and remainders.
  • Write a main function that prompts the user to enter an integer (in seconds) and calls the convert_seconds function with the input as an argument.

Here is how our program looks like:

#include <stdio.h>

// A function to convert a given integer (in seconds) to hours, minutes and seconds

void convert_seconds(int seconds) {

  // Declare some variables to store the hours, minutes and seconds

  int h, m, s;

  // Initialize the variables to zero

  h = m = s = 0;

  // Use a while loop to divide the seconds by 3600, 60 and 1 respectively and store the quotients and remainders

  while (seconds > 0) {

    // If the seconds are greater than or equal to 3600, divide by 3600 and store the quotient in h and the remainder in seconds

    if (seconds >= 3600) {

      h = seconds / 3600;

      seconds = seconds % 3600;

    }

    // Else if the seconds are greater than or equal to 60, divide by 60 and store the quotient in m and the remainder in seconds

    else if (seconds >= 60) {

      m = seconds / 60;

      seconds = seconds % 60;

    }

    // Else if the seconds are greater than or equal to 1, store the value in s and set seconds to zero

    else if (seconds >= 1) {

      s = seconds;

      seconds = 0;

    }

  }

  // Print the hours, minutes and seconds to the standard output

  printf("%d seconds = %d hours, %d minutes and %d seconds\n", seconds, h, m, s);

}

// A main function to test the convert_seconds function

int main() {

  // Declare a variable to store the input

  int input;

  // Prompt the user to enter an integer (in seconds)

  printf("Enter an integer (in seconds): ");

  scanf("%d", &input);

  // Call the convert_seconds function with the input as an argument

  convert_seconds(input);

}

Output

The output of the above program depends on the input that the user enters. For example, if the user enters 5000, the output will be:

5000 seconds = 1 hour, 23 minutes and 20 seconds

Explanation

The explanation of the above program is as follows: The program consists of two functions: a convert_seconds function and a main function.

  • The convert_seconds function takes one parameter (the number of seconds) and prints the hours, minutes and seconds that correspond to that number.
  • The convert_seconds function uses a while loop and a series of if-else statements to divide the number of seconds by 3600, 60 and 1 respectively and store the quotients and remainders in variables h, m and s.
  • The convert_seconds function then prints the values of h, m and s to the standard output using a printf statement.
  • The main function declares a variable input to store the input from the user.
  • The main function prompts the user to enter an integer (in seconds) using a printf statement and reads the input using a scanf statement.
  • The main function then calls the convert_seconds function with the input as an argument.
  • The program ends when the main function returns 0.

Conclusion

In this program, I have shown you how to write a C program that can convert any integer (in seconds) to hours, minutes and seconds using a while loop and a series of if-else statements. I hope you have learned something useful and enjoyed reading this post. If you have any questions or feedback, please leave a comment below. Thank you for reading!



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld