Time Conversion Program Using C


Description

This program is about conversion of user input data into 24 hour time format also updation of time on user demand.



#include<stdio.h>
struct time_struct
{
    int hour;
    int minute;
    int second;
};
struct time_struct input(struct time_struct);

struct time_struct update(struct time_struct);

int main()
{
 struct time_struct t,t1,t2;
 t2.hour=t2.minute=t2.second=0;
 printf("Default time is 0 : 0 : 0\n");
 int choice;
 do
 {
  printf("\nMENU\n1.input time\n2.update time\n3.EXIT\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1: t2=input(t);break;
   case 2:t1=update(t2);
           t2=t1;break;
   case 3:printf("~~~~ THANK YOU ~~~~\n");break;
   default:printf("INVALID input\n");
  }
 }while(choice!=3);
 
}
struct time_struct input(struct time_struct t)
{
   
   printf("enter hours\nminutes\nseconds repectively\n");
   scanf("%d%d%d",&t.hour,&t.minute,&t.second);
   if(t.hour>24||t.minute>59||t.second>59)
   printf("INVALID TIME !!!\n");
   else
   printf("THE TIME is: \n%d : %d : %d\n",t.hour,t.minute,t.second);
   return t;
}

struct time_struct update(struct time_struct t)
{
   t.second++;
   if(t.second==60)
   {
      t.second=0;
      t.minute++;
   }
   if(t.minute==60)
   {
      t.minute=0;
      t.hour++;
   }
   if(t.hour==24)
   t.hour=0;
    printf("THE UPDATED TIME is:\n");
    printf("%d : %d : %d\n\n",t.hour,t.minute,t.second);
    return t;
}

Labels: