Sunday, August 2, 2009

Write a program in C?

how would you go about writing a C program where the user is asked to enter seconds. The program should convert the seconds into days, hours and minutes?





To get hours you divide by 3600 seconds, to get days you divide by 86400 seconds, and to get minutes you divide by 60 seconds.

Write a program in C?
I hope this is not your class assignment, so here goes





int main()


{


long seconds;


int minutes, hours, days;





printf("Enter seconds:");


scanf("%ld", %26amp;seconds);





$minutes = seconds / 60;


$seconds = seconds%60;


$hours = $minutes / 60;


$minutes = $minutes % 60;


$days =$hours/ 24;


$hours = $hours%24;





printf("\n\n%d days, %d hours, %d minutes, %d seconds", $days, hours, minutes, seconds);


}
Reply:let's write function for each of the conversion differently





void main()


{


long int sec,choice,ans;


clrscr();


println("Enter the second");


scanf("%d",%26amp;sec);


convert(sec);


getch();


}





void convert(long int s)


{


long int day=0, hrs=0, min=0;





day=s/86400; // Convert the seconds into days


s = s- day*86400; //calculate the remaining second after convertion into days





hrs = s / 3600; // Convert the seconds into hours


s = s - hrs*3600; //calculate the remaining second after convertion into hours





min = s/60; // Convert the seconds into minutes


s = s - min * 60; //calculate the remaining second after convertion into minutes





printf("After convertion of seconds the result is: \n")


printf("%ld Days, %ld Hours , %ld Minutes and %ld Seconds", day,hrs,min,s);


}


No comments:

Post a Comment