Calculating Hours, Minutes And Seconds in C

Introduction

In this blog, I am going to explain how to calculate the seconds, minutes and hours by using the given second.

Software Requirement

Turbo C++ or C. 

Programming

  1. #include < stdio.h >   
  2. #include < conio.h > void main()  
  3. {  
  4.     int sec, hr = 0, min;  
  5.     clrscr();  
  6.     printf("\n Enter seconds:");  
  7.     scanf("%d", & sec);  
  8.     min = sec / 60;  
  9.     sec = sec % 60;  
  10.     if (min >= 60)  
  11.     {  
  12.         hr = min / 60;  
  13.         min = min % 60;  
  14.     }  
  15.     if (hr != 0)  
  16.     {  
  17.         printf("\n No of hours:%d", hr);  
  18.         printf("\n No of minutes:%d", min);  
  19.         printf("\n No of seconds:%d", sec);  
  20.     } else if (hr == 0)  
  21.     {  
  22.         printf("\n No of Minutes:%d", hr);  
  23.         printf("\n No of seconds:%d", min);  
  24.     }  
  25.     getch();  
  26. }  
Explanation

In the programming, given above, I explain in a clear manner, how to print the hours, seconds and minutes. 

programming

programming

Output

Output

Conclusion

Thus, it is a program for calculating the hours, minutes and seconds by using the given seconds, which can be executed and printed successfully.
Ebook Download
View all
Learn
View all