Question

Create the Code using C Program: 2. Write a program to convert the time from 24-hour...

Create the Code using C Program:
2. Write a program to convert the time from 24-hour notation to 12-hour notation and vice-versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results (For 12-hour time notation, your program must display AM or PM)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<stdio.h>
#include<string.h>
int menu();
void take24HourInput(int *,int*,int*);
void convert24To12(int *,char []);
void print12HourFormat(int ,int ,int,char);
void take12HourInput(int *,int*,int*,char);
void convert12To24(int *,char []);
void print24HourFormat(int ,int ,int);
int main()
{
   int choice;
   int hh, mm, ss;
char a[3];
   do
   {
       choice=menu();
       if(choice==1)
       {
           take24HourInput(&hh,&mm,&ss);
           convert24To12(&hh,a);
           print12HourFormat(hh,mm,ss,a);
       }
       else if(choice==2)
       {
           take12HourInput(&hh,&mm,&ss,a);
           convert12To24(&hh,a);
           print24HourFormat(hh,mm,ss);
       }
   }while(choice!=0);
}
int menu()
{
   int choice;
   printf("1) Convert the time from 24-hour notation to 12-hour notation\n");
   printf("2) Convert the time from 12-hour notation to 24-hour notation\n");
   printf("0) Exit\n");
   printf("Enter your choice: ");
   scanf("%d",&choice);

}
void take24HourInput(int *hh,int *mm,int *ss)
{
   int h,m,s;
   printf("Enter hours 'hh' \t");
scanf("%d", &h);
printf("Enter minutes 'mm' \t");
scanf("%d", &m);
printf("Enter seconds 'ss' \t");
scanf("%d", &s);
   *hh=h;
   *mm=m;
   *ss=s;
}
void take12HourInput(int *hh,int *mm,int *ss,char a[])
{
   int h,m,s;
   printf("Enter hours 'hh' \t");
scanf("%d", &h);
printf("Enter minutes 'mm' \t");
scanf("%d", &m);
printf("Enter seconds 'ss' \t");
scanf("%d", &s);
printf("Enter string 'am' or 'pm' \t");
scanf("%s", &a);
   *hh=h;
   *mm=m;
   *ss=s;
}
void convert24To12(int *h,char a[])
{
   if(*h==0 || *h==24)
   {
       *h=12;
   }
   else if(*h>12)
   {
       *h-=12;
       strcpy(a,"PM");
   }
   else
   {
       strcpy(a,"AM");
   }
}
void convert12To24(int *h,char a[])
{
       if((strcmp(a,"PM") == 0) || (strcmp(a,"pm") == 0) && (h != 0) && (*h != 12))
{
*h = *h + 12;
}
if((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0) && (*h == 12))
{
*h = 0;
}
}
void print12HourFormat(int hh,int mm,int ss,char a[])
{
   printf("\n12 Hour Format: ");
   printf("\n%02d:%02d:%02d %s\n\n",hh,mm,ss,a);
}
void print24HourFormat(int hh,int mm,int ss)
{
   printf("\n24 Hour Format: ");
   printf("\n%02d:%02d:%02d %s\n\n",hh,mm,ss);
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Create the Code using C Program: 2. Write a program to convert the time from 24-hour...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...

    1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the...

  • Need some assistance of reorganizing this whole program. I have the right code for everything I...

    Need some assistance of reorganizing this whole program. I have the right code for everything I just need help on putting all the codes in the right spot so it can come out to the correct output. output is supposed to look like this: 1 \\ user inputs choice to convert 12 to 24 8 \\ user inputs 8 for hours 30 \\ user inputs 30 for minutes 20 \\ user inputs 20 for seconds AM \\ user inputs AM...

  • C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it...

    C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the...

  • in C++ Write a program that converts a time in 12-hour format to 24-hour format. The...

    in C++ Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the user to enter a time in HH:MM:SS AM/PM form. (The time must be entered exactly in this format all on one line.) It will then convert the time to 24 hour form. You may use a string type to read in the entire time at once including the space or you may choose to use separate variables for the hours,...

  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...

  • Write a program (C++) that requests the current time and a waiting time as two integers...

    Write a program (C++) that requests the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the times. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants to end the program.You can assume the wait time will always...

  • I have this c++ program that i have to create but i am stuck on it....

    I have this c++ program that i have to create but i am stuck on it. Write a height conversion program that shall allow user to convert from feet and inches to meters (option 1) and vice versa (option 2). User shall be able to specify the type of conversion (a menu of two options). Display an error message if an invalid option is specified. Otherwise, the program would read in a length in feet and inches (two separate integer...

  • I need help with a Python program that has error checking loops. Hour must be a...

    I need help with a Python program that has error checking loops. Hour must be a number from 1 to 12. Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered. Every time an invalid value is entered, display an error message and ask the user to re-enter a valid value immediately. Functions are not covered yet and are not necessary. Thanks. The following is an example: Enter hour: 0 Hour must...

  • POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think...

    POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think about them differently. Some people and places in the world work with a 24-hour clock that goes from 0:00 to 23:59, while others work with a 12-hour clock that goes from 12.00AM to 11:59PM 24-hour clock 0.00 (midnight) 1200 (noon) 23:59 (one minute before midnight), A COLLAPSE 12-hour clock: *12.00 AM" (midnight) *12:00 PM" (noon) "11:59 PM" (one minute before midnight), You are going...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT