12. Program that converts MILITARY time to STANDARD time.

Chapter 5: 

Conditional Structures

Programming Exercise

Problem # 12:

Write a program that converts MILITARY time to STANDARD time.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int hours,mints,secs;
    cout<<"......This Program converts Military Time to Standard Time...... "<<endl;
    cout<<"\nEnter Military Time in hh:mm:ss format : "<<endl;
    cin>>hours>>mints>>secs;
    if(hours>=13 && hours<=24 && mints>=0 && mints<=60 && secs>=0 && secs<=60)
    {
        hours=hours-12;
        cout<<hours<<":"<<mints<<":"<<secs<<" PM";
    }
    else if(hours>0 && hours<=12 && mints>=0 && mints<=60 && secs>=0 && secs<=60)
    {
        cout<<hours<<":"<<mints<<":"<<secs<<" AM";
    }
    else if(hours==0 && mints>=0 && mints<=60 && secs>=0 && secs<=60)
    {
        hours=hours+0;
        cout<<"12:"<<mints<<":"<<secs<<" AM";
    }
    else
        cout<<"Wrong Entry";
    return 0;
}

OR

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int t,h,m,H;
    cout<<"Enter Military time=";
    cin>>t;
    h=t/100;
    m=t%100;
    if((h>=0&&h<=23)&&(m>=0&&m<=59))
    {
        if(h>12)
        {
            H=h%12;
            cout<<"The standard time of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"PM";
         }
         else if(h==0)
         {
             H=12;
             cout<<"The standard time of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"AM";
         }
        else
        {
            H=h;
            cout<<"The standard time of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"AM";  
        }  
    }
    else
    cout<<"wrong input.";  
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
11. Program that inputs obtained marks of a student, calculates the percentage and displays grade.

Comments