![PastelHibiscus](/avatars/8482.jpg)
PastelHibiscus
13.09.2019 •
Computers and Technology
Write a program to calculate the number of seconds since midnight. for example, suppose the time is 1: 02: 05 am. since there are 3600 seconds per hour and 60 seconds per minutes, it has been 3725 seconds since midnight (3600 * 1 + 2 * 60 + 5 = 3725). the program asks the user to enter 4 pieces of information: hour, minute, second, and am/pm. the program will calculate and display the number of seconds since midnight. [hint: be very careful when the hour is 12].
Solved
Show answers
More tips
- S Style and Beauty How to choose the best mascara for your eyelashes...
- F Food and Cooking Discover Delicious Recipes You Can Make with Ground Meat...
- C Computers and Internet Google Search Tips and Tricks: Everything You Need to Know...
- S Science and Technology Why is there no gravity on other planets?...
- L Leisure and Entertainment How to Properly Wind Fishing Line onto a Reel?...
- L Leisure and Entertainment How to Make a Paper Boat in Simple Steps...
- T Travel and tourism Maldives Adventures: What is the Best Season to Visit the Luxurious Beaches?...
- H Health and Medicine Kinesiology: What is it and How Does it Work?...
- O Other How to Choose the Best Answer to Your Question on The Grand Question ?...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
Ответ:
// here is code in c++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// variables
int hour,min,sec,tot_sec;
string str,s="AM";
cout<<"enter number of hours:";
// read hour
cin>>hour;
cout<<"enter number of minutes:";
// read minute
cin>>min;
cout<<"enter number of seconds:";
// read seconds
cin>>sec;
cout<<"Enter AM or PM:";
// read AM or PM
cin>>str;
if((str.compare(s) == 0 )&& hour==12)
{
hour=0;
}
// calculate total seconds
tot_sec=hour*3600+min*60+sec;
// print the output
cout<<"total seconds since midnight is : "<<tot_sec<<endl;
return 0;
}
Explanation:
Read the value of hour, minute, second and string "AM" or "PM" from user.
Then if string is equal to "AM" then make hour=0.Then multiply hour with
3600, minute with 60 and add them with second.It will give the total seconds
from midnight.
Output:
enter number of hours:12
enter number of minutes:46
enter number of seconds:23
Enter AM or PM:AM
total seconds since midnight is: 2783
Ответ: