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

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

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

  • Python Programing : Convert to binary - functions Write a program that takes in a positive...

    Python Programing : Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a...

  • Write a program in C that will convert all 12 months' average temperature from Celsius to...

    Write a program in C that will convert all 12 months' average temperature from Celsius to Fahrenheit. You need to create an array named aye jump and prompt the user to input average temperature in Celsius for all 12 months. After that, you need to develop a user temperatures from Celsius to Fahrenheit and pass the array as reference and convert all temperatures from Celsius to Fahrenheit. Next, compute the average temperature of the year and assign it to a...

  • Write a Java program that will implement a stack object to convert from either infix notation...

    Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation.  The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output: "Please select what type of conversion you would like to do: Infix to postfix Postfix to infix Print Equations Exit"

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