Question

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 input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)

2

Write a function that takes as a parameter an integer (as a long long value) and returns the number of odd, even, and zero digits

Note:

Note write c++ program for the above question using functions do display the output the program should work for each and ever

do attempt both the question as they are on the same topic functions

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

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


Answer 1:
=========

#include <iostream>
#include <string>
using namespace std;

void convertTo24Hr(int h_in, int m_in, string ampm, int &h_out, int &m_out);
void convertTo12Hr(int h_in, int m_in, int &h_out, int &m_out, string &ampm);
void display(string message, int h, int m, string ampm = "");
int menu();

int main(){
   int choice;
   int h_in, h_out, m_in, m_out;
   string ampm;
   char colon;
  
   while((choice = menu()) != 3){
       switch(choice){
           case 1:
               cout << "Enter the time in 24-hour format (hh:mm): ";
               cin >> h_in >> colon >> m_in;
               convertTo12Hr(h_in, m_in, h_out, m_out, ampm);
               display("The time in 12-hour format is ", h_out, m_out, ampm);
               break;
           case 2:
               cout << "Enter the time in 12-hour format (hh:mm am/pm): ";
               cin >> h_in >> colon >> m_in >> ampm;
               convertTo24Hr(h_in, m_in, ampm, h_out, m_out);
               display("The time in 24-hour format is ", h_out, m_out);
               break;
       }
   }
   return 0;
}

void convertTo24Hr(int h_in, int m_in, string ampm, int &h_out, int &m_out){
   h_out = h_in;
   m_out = m_in;
  
   if(ampm == "pm" || ampm == "PM")
       h_out += 12;
}

void convertTo12Hr(int h_in, int m_in, int &h_out, int &m_out, string &ampm){
   h_out = h_in;
   m_out = m_in;
   ampm = "AM";
   if(h_out > 12){
       h_out = h_out - 12;
       ampm = "PM";
   }
   else if(h_out == 12)
       ampm = "PM";
}

void display(string message, int h, int m, string ampm){
   cout << message;
   if(h < 10)
       cout << "0";
   cout << h << ":";
   if(m < 10)
       cout << "0";
   cout << m;
  
   if(ampm != "")
       cout << " " << ampm;
   cout << endl;
}
int menu(){
   int choice;
   do{
       cout << "1. Convert from 24-hour to 12-hour notation" << endl;
       cout << "2. Convert from 12-hour to 24-hour notation" << endl;
       cout << "3. Exit" << endl;
       cout << "Enter your choice: ";
       cin >> choice;
   }while(choice < 1 || choice > 3);
}

output
====
1. Convert from 24-hour to 12-hour notation
2. Convert from 12-hour to 24-hour notation
3. Exit
Enter your choice: 1
Enter the time in 24-hour format (hh:mm): 14:30
The time in 12-hour format is 02:30 PM
1. Convert from 24-hour to 12-hour notation
2. Convert from 12-hour to 24-hour notation
3. Exit
Enter your choice: 2
Enter the time in 12-hour format (hh:mm am/pm): 5:45 pm
The time in 24-hour format is 17:45
1. Convert from 24-hour to 12-hour notation
2. Convert from 12-hour to 24-hour notation
3. ExitEnter your choice: 3

Answer 2
========
#include <iostream>
#include <string>
using namespace std;
void countDigits(long long num, int &odd, int &even, int &zero);
int main(){
   long long num;
   int odd, even, zero;
  
   cout << "Enter a number: ";
   cin >> num;
   countDigits(num, odd, even, zero);
   cout << "No. of odd digits: " << odd << endl;
   cout << "No. of even digits: " << even << endl;
   cout << "No. of zeros: " << zero << endl;
   return 0;
}

void countDigits(long long num, int &odd, int &even, int &zero){
   int rem;
   odd = 0;
   even = 0;
   zero = 0;
   while(num != 0){
       rem = num % 10;
       if(rem == 0)
           zero++;
       else if(rem % 2 == 0)
           even++;
       else if(rem % 2 == 1)
           odd++;
       num = num / 10;
   }
}

output
-------
Enter a number: 1230450
No. of odd digits: 3
No. of even digits: 2
No. of zeros: 2

Add a comment
Know the answer?
Add Answer to:
1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...
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
  • 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...

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

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

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

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

  • Time Displacement (12-hour format) Write a program that requests the current time and a waiting time...

    Time Displacement (12-hour format) Write a program 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 input time and 12-hour notation for the output time. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants...

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

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • Write a program in c++ to convert an expression written in infix notation to be converted...

    Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....

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