Pages

Thursday, June 27, 2013

C++ Structures Time Addition code

QUESTION :
Time Addition
Use a time structure having three members hours, min and seconds of type int. Write a

program that obtain two time values form user in format 12:59:59, store them in struct

time. Convert each time to seconds and add theses quantities, and convert the result back

to hours, min and seconds and final display the result in 12:59:59 format.

CODE:
/////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
struct time{
int hours;
int mins;
int seconds;
};
int main(){
long int sum,sum1,sum2;
time s1,s2;
long int h,m,s;
cout<<"\nEnter Hours\n";
cin>>s1.hours;
cout<<"\nEnter Mins\n";
cin>>s1.mins;
cout<<"\nEnter seconds\n";
cin>>s1.seconds;
sum = (s1.hours*3600)+(s1.mins*60)+(s1.seconds);
cout<<"\nEnter Hours\n";
cin>>s2.hours;
cout<<"\nEnter Mins\n";
cin>>s2.mins;
cout<<"\nEnter seconds\n";
cin>>s2.seconds;
sum += (s2.hours*3600)+(s2.mins*60)+(s2.seconds);
h = sum/3600;
sum1 = sum%3600;
m = sum1/60;
sum2 = sum1%60;
s =sum2;
cout<<"Time is"<<h<<":"<<m<<":"<<s<<endl;
 return 0;
}




/////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment