SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:3 | ISBN:9780321531346 | Edition: 7

Question

Modify your program for Programming Project 2 so that it uses 12 hour notation, such as 3:45 PM.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include<iostream>
using namespace std;
void main()
{  

	int currHours,currMins,hoursWait,minutesWait;
	int hrs,min;
	char ch;
	do
	{
      cout<<"Enter present Hours:";
	  cin>>currHours;
	  cout<<"Enter present minutes:";
	  cin>>currMins;	
 	  cout<<"Enterthe number of hours to wait:";
	  cin>>hoursWait;
	  cout<<"Enter the number of minutes to wait:";
	  cin>>minutesWait;
      hrs=currHours+hoursWait;
	  min=currMins+minutesWait;
	  if(min>=60)
	  {
		  hrs++;
		  min =min-60;
	  }
	  if(hrs>12)
	  {
         hrs=hrs%12;
		  cout<<"Time:"<<hrs<<":"<<min<<"PM"<<endl;
	  }
	  else
	  {
		  	  cout<<"Time:"<<hrs<<":"<<min<<"AM"<<endl;
	  }
		cout<<"Enter y or Y to continue...";
	  cin>>ch;
     }while(ch == 'y' || ch =='Y');
}

Output:

Enter present Hours:19
Enter present minutes:30
Enterthe number of hours to wait:20
Enter the number of minutes to wait:15
Time:3:45PM
Enter y or Y to continue...n

 

0 0

Discussions

Post the discussion to improve the above solution.