AIM:-   Write Program For Time class Using Programming c++.

 

Iinput:-

 

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

class dhaval
{
int h=0,m=0,s=0,n_h,n_m,n_s,i,j,k;
char am_pm[2];

public:

void accept()
{
cout<<“enter 12 or 24:”;
cin>>i;
}

void calculate()
{
if(i==12)
{
cout<<“enter n_s:”;
cin>>n_s;
cout<<“enter n_m:”;
cin>>n_m;
cout<<“enter n_h:”;
cin>>n_h;
cout<<“enter am or pm:”;
cin>>am_pm;

h+=n_h;
m+=n_m;
s+=n_s;

if(h>12)
{
h=n_h-12;

}
if(m>60)
{
m=n_m-60;
h++;

}
if(s>60)
{
s=n_s-60;
m++;

}
}

else if (i==24)
{
cout<<“enter n_s:”;
cin>>n_s;
cout<<“enter n_m:”;
cin>>n_m;
cout<<“enter n_h:”;
cin>>n_h;

n_h+=h;
n_m+=m;
s+=n_s;

if(h>25)
{
h=n_h-24;

}
if(m>61)
{
m=n_m-60;
h++;

}
if(s>61)
{
s=n_s-60;
m++;

}
}
}

void display()
{
if(i==12)
{

cout<<“”<<h<<“:”<<m<<“:”<<s<<” :”<<am_pm;
cout<<“\n”;

}

if(i==24)
{
cout<<“”<<h<<“:”<<m<<“:”<<s;
cout<<“\n”;

}
}

};

int main()
{
dhaval Dha;
Dha.accept();
Dha.calculate();
Dha.display();

return 0;

}

 

Output:-

enter 12 or 24:12
enter n_s:69
enter n_m:65
enter n_h:14
enter am or pm:pm
3:6:9 :pm

 

 

Advertisements