Seconds Converter

+1 Haris Razis · August 25, 2015
Hey guys,

The seconds are not displayed no matter the input.I cant see the problem,any help welcome.


int main()
    int Hours, Min, Secs1, Secs2;

    printf("Please enter the seconds\n");
    scanf(" %d", &Secs1);

    Hours = Secs1/3600;
    Min = (Secs1%3600)/60;
    Secs2 = (Min*60)%60;

    printf("%d Hours %d Min %d Secs", Hours, Min, Secs2);

    return 0;

Post a Reply


Oldest  Newest  Rating
0 Trippy A-K · August 26, 2015
Hello and here are two tips, and a pic of a solution algorithm.
1) Plan ahead when making a program. In this program you took your sec1, did some math, and made it into sec2, but this is unnecessary because you are taking in seconds already, so just pass the original sec1 intot he print statement.
2) Since you are doing division and will be encountering fractions if secs are any value, you should use floats, not ints


int main(){
    float h,m,s;

    printf("Enter seconds\n");

    scanf("%f", &s);

    h = s/3600;
    m = s/60;

    printf("%.2f %.2f %.2f",h,m,s);
    return 0;
0 Franz Schmidt · August 26, 2015
Your problem is the calculation of the secs2 variable you made a logical mistake:

you have 2 mins and the calculation should also give 5 secs so this would be 125 secs which you would need to enter.
when you whould just multiply the mins with 60 (means 2*60=120) where never will remain a remainder cause 120 is always divideable by 60 and every other number which is multiplied by 60.

So you need to modulate the total secs1 to get the right result:
Secs2 = Secs1 % 60;
0 Haris Razis · August 27, 2015
Thank you guys,both of the answers were helpful. @Trippy A-K i want to display ex that 4000 secs are 1 hours 6 mins 40 secs.Thats why i used the 2nd variable for secs 
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator