Tuesday, April 30, 2013

c Program to Convert Time in Seconds to Hours, Minutes and Seconds


/*
Program to Convert Time in Seconds to Hours, Minutes and Seconds
*/
#include <stdio.h>
main()
{
long sec, hr, min, t;
printf("\nEnter time in seconds: ");
scanf("%ld", &sec);
hr = sec/3600;
t = sec%3600;
min = t/60;
sec = t%60;
printf("\n\nTime is %ld hrs %ld mins %ld secs", hr, min, sec);
getch();
}
hr = sec/3600;
This program follows the general logic of converting second to hour.ie divide the total second by 3600 thus we get the Hour,
 t = sec%3600
Then make a modular division on the total second ,thus we get the remaining seconds that can not form a hour.
min = t/60;
divide the 't' (seconds)with 60 ,then we get the remaining minutes.
sec = t%60;
modular division on the 't' result in the remaining seconds....

19 comments:

  1. Replies
    1. 60*60=3600,cause change sec into hour

      Delete
  2. 3600 may be 60 sec in a min and 60 min in a hour

    ReplyDelete
  3. I just wanted to covert second into min and sec, and the following code creating run time error kindly help.

    #include
    main(){
    long min, sec,time;
    printf("Enter the desired second you want to convert :");
    scanf("%ld",time);
    min=time/60;
    sec=time%60;
    printf("The converted time is %ld:%ld",&min,&sec);
    }

    ReplyDelete
    Replies
    1. mistake 1 : first line should be "#include ". you did not mention the stdio part
      mistake 2 : fifth line should be "scanf("%ld",&time);".dont forge the &
      mistake 3 : eight line should be "printf("The converted time is %ld:%ld",min,sec);". you shouldn"t keep & here

      Delete
    2. #inlude
      #include
      void main()
      {
      clrsrc();
      int a,b,c;
      printf("\nenter hour\n")
      scanf(%d",&a);
      b=a*60;
      c=b*60;
      printf("\n the convert hour is%d",c)
      getch();
      }

      Delete
  4. printf("The converted time is %ld:%ld",&min,&sec);

    using &min &sec here is wrong! Those are the addresses, not the values.

    ReplyDelete
  5. I am proud of your site, & interested.

    Job Typing

    ReplyDelete
  6. hello friends
    I just want algorithm of this coding

    ReplyDelete
  7. can you help me to describe the difference between \ and / operator ?



    #include
    #include
    main()
    {
    int sec, mint, hr;
    printf("\n enter time in seconds");
    scanf("%d",&sec);
    hr=sec/3600;
    sec=sec%3600;
    mint=sec/60;
    sec=sec%60;
    printf("\n %d : %d : %d",hr,mint,sec);
    getch();
    }

    ReplyDelete
    Replies
    1. I have wrote the header files but it was not shown, why?

      Delete
    2. I have wrote the header files but it was not shown, why?

      Delete
  8. after doing all this in dev c++ my answer is wrong .please guide my mistake
    #include
    using namespace std;
    int main()
    {
    char time;
    double second,hours,minutes;
    cout<<"please enter the time";
    cin>>time;
    hours=time/;
    time=time%3600;
    minutes=time/60;
    time=time%60;
    second=time;

    cout<<hours<<" "<<"hours"<<minutes<<" "<<"minutes"<<second <<" "<<"second"<<endl;

    }

    ReplyDelete

  9. The competition
    The Sensitive Society Club organizes a donation campaign. To put more fun in the campaign, they organize a competition among departments. In the competition top donating students and the department are awarded with a certificate.

    Question: Write a program that reads the information for n students then it shows

    the name of the student who donated the most amount
    and the department whose average is the highest
    Input specification: You will be given one integer (n) in the beginning. Then, the following n lines will have three information:

    Student name: names are at most 20 char strings containing only 26 English (uppercase or lowercase) letters
    his/her department: at most 4 chars long string (only 3 departments in the competition: CEN, ECE or BINF)
    The amount donated: a positive integer not greater than 20,000
    where n is between 1 and 10,000.
    Output specification: Show two strings:

    the name of the student who donated the most,
    The department whose average is the highest.
    Note: If there are two students with the same value, show the first one.
    Sample Input
    6
    Alban CEN 15
    Redi ECE 7
    Erda CEN 4
    Kei BINF 9
    Ardi BINF 11
    Arli ECE 12

    Sample Output
    Alban BINF

    ReplyDelete
  10. Accept min from user and convert in hours, min and sec.
    Please help me.

    ReplyDelete
  11. #include
    #include
    void main()
    {
    int sec, mint, hr;
    printf("\n enter time in seconds");
    scanf("%d",&sec);
    hr=sec/3600;
    sec=sec%3600;
    mint=sec/60;
    sec=sec%60;
    printf("\n %d : %d : %d",hr,mint,sec);
    getch();
    }

    ReplyDelete
  12. Design a program to find number of minutes and number of seconds from given number of
    years.

    ReplyDelete