Professional Documents
Culture Documents
045 (Maham)
045 (Maham)
Q1.Create a structure called time. Its three members, all type int, should be
called hours, minutes, and seconds. Write a program that prompts the user to
enter a time value in hours, minutes, and seconds. This can be in 12:59:59
format, or each number can be entered at a separate prompt (“Enter hours:”,
and so forth). The program should then store the time in a variable of type
struct time, and finally print out the total number of seconds represented by
this time value: long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
Program:
// adding libraries
using namespace std;
#include<iostream>
// making a structure class
struct time{
int hours,mint,sec;
};
// main part of program
int main()
{
time t;
int total_sec;
cout<<"Enter the Hours:";
cin>>t.hours;
cout<<"Enter the minutes:";
cin>>t.mint;
cout<<"Enter seconds:";
cin>>t.sec;
cout<<"Your entered time is "<<t.hours<<":"<<t.mint<<":"<<t.sec<<endl;
total_sec=t.hours*3600+t.mint*60+t.sec;
cout<<"Total second in your entered time is: "<<total_sec;
cout<<"program done by 045(maham)"<<endl;
}
int main(){
struct Employee employee[3];
//Ask the user to fill in this data for three employees store it in three
variables of type struct employee
for(int i=0;i<3;i++){
cout<<"Enter the employee number "<<(i+1)<<": ";
cin>>employee[i].number;
cout<<"Enter the employee compensation "<<(i+1)<<": ";
cin>>employee[i].compensation;
cout<<"\n";
}
//display the information for each employee.
cout<<"\n";
for(int i=0;i<3;i++){
cout<<"The employee number "<<(i+1)<<":
"<<employee[i].number<<"\n";
cout<<"The employee compensation "<<(i+1)<<":
"<<employee[i].compensation<<"\n";
}
return 0;
}
Q3.Create a class called Triangle that stores the length of the base and height
of a right triangle in two private instance variables. Include a constructor that
sets these values. Define two functions. The first is hypot( ), which returns the
length of the hypotenuse. The second is an area( ), which returns the area of
the triangle.
Note: Hypot= sqrt((length*length) + (height*height))
Area = sqrt((length*length) + (height*height))
Program:
//adding libraries
#include <iostream>
using namespace std;
// creating class named triangle
class triangle{
public:
int height;
int length;
float hyp;
float ar;
triangle(int l, int h){
length=l;
height=h;
}
//function for hypotenuse
void hypotenuse(){
hyp=((length*length)+(height*height));
cout<<"hypotneuse = "<<hyp<<endl;
}
//function for area
void area(){
ar=(length*height)/2;
cout<<"area = "<<ar<<endl;
}
};
int main() {
int l,h;
cout<<"enter length of the base of the right angled triangle: "<<endl;
cin>>l;
cout<<"enter height the right angled triangle: "<<endl;
cin>>h;
triangle t1(l,h);
t1.hypotenuse();
t1.area();
return 0;
}
#include <iostream>
using namespace std;
class Tollboth //class declaretion
{
//attributes
int cars;
double collect;
public:
//contstructor
Tollboth()
{
cars=0;
collect=0;
}
//methods
void payingcar() //paying car method
{
cars++;
collect+=0.50;
cout<<"Entry Succesfully added."<<endl;
}