Dremendo Tag Line

Convert time in seconds in C

scanf() - Question 16

In this question, we will see how to input time in hours, minutes and seconds in C programming using the scanf() function and print the total time only in seconds. To know more about scanf() function click on the scanf() function lesson.

Q16) Write a program in C to input time in hours, minutes and seconds and print the total time only in seconds.

Hour: 1
Minutes: 10
Seconds: 6
Total Seconds: 4206

Program

#include <stdio.h>
#include <conio.h>

int main()
{
    int h,m,s,ts;
    printf("Enter hours ");
    scanf("%d",&h);
    printf("Enter minutes ");
    scanf("%d",&m);
    printf("Enter seconds ");
    scanf("%d",&s);
    ts=(h*3600)+(m*60)+s;
    printf("Total time in seconds=%d",ts);
    return 0;
}

Output

Enter hours 1
Enter minutes 20
Enter seconds 30
Total time in seconds=4830
video-poster