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...

  • You want to write a converter program which will let user convert distance and weight from SI to ...

    Use functions to complete this C + + program You want to write a converter program which will let user convert distance and weight from SI to the USCS system and vice versa The formulae are as follows: 1. Distance in miles- distance in kilometers x 1.60 2. Weight in kilograms weight in pounds x 2.20 The program would let user input the number that they want to convert and then choose the units to and from which they want...

  • 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...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • 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...

  • Language: C# In this assignment we are going to convert weight and height. So, the user...

    Language: C# In this assignment we are going to convert weight and height. So, the user will have the ability to convert either weight or height and as many times as they want. There conversions will only be one way. By that I mean that you will only convert Pounds to Kilograms and Feet and Inches to Centimeters. NOT the other direction (i.e. to Pounds). There will be 3 options that do the conversion, one for each type of loop....

  • 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...

  • Please design, write the code and create the requisite submission files in C++ for the following...

    Please design, write the code and create the requisite submission files in C++ for the following problem: A menu-driven program that offers the user 5 options as follows: Menu: 1. Display array 2. Add up all elements in the array and display that number. 3. Add 1 to each element in the array. 4. Swap the values in row one with the values in row three. 5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x...

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
ADVERTISEMENT