WAP TO ADD TWO TIME AND ADD THEM & DISPLAY THE TOTAL TIME
#include<conio.h>
#include<iostream.h>
class time
{
int hr,min;
public:
void set_time(int h,int m)
{
hr=h;
min=m;
}
void disp()
{
cout<<hr<<":"<<min;
}
void add(time,time);
};
void time::add(time t1,time t2)
{
hr=t1.hr+t2.hr;
min=t1.min+t2.min;
if(min>60)
{
hr+=1;
min-=60;
}
}
void main()
{
clrscr();
time t1,t2,t3;
t1.set_time(5,30);
t2.set_time(3,70);
t3.add(t1,t2);
t1.disp();
cout<<endl;
t2.disp();
cout<<endl;
t3.disp();
getch();
}
Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts
Monday, October 10, 2011
FUNCTIONS_4
WAP TO INPUT BANK DETAILS OF A CUSTOMER AND ALLOW WITHDRAW IF THERE IS SUFFICIENT BALANCE
FIRST METHOD
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class bank
{
char name[10],type;
int acno;
float amt,dep,bal;
public:
void init();
void depo();
void with();
void disp();
};
void bank::init()
{
cout<<"Name::";
cin>>name;
cout<<"Enter s for savings or C for current account::";
cin>>type;
}
void bank::depo()
{
cout<<"Account no & amt to deposit::";
cin>>acno>>dep;
bal=0;
bal+=dep;
}
void bank::with()
{
cout<<"Amt to withdraw::";
cin>>amt;
if(amt>bal)
cout<<"Insufficient balance";
else
bal-=amt;
}
void bank::disp()
{
cout<<"Name::"<<name<<endl
<<"Balance::"<<bal<<endl;
}
void main()
{
clrscr();
bank b;
b.init();
b.depo();
b.with();
b.disp();
getch();
}
ANOTHER WAY TO SOLVE THE PROBLEM
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class bank
{
char name[10],type;
int acno;
float amt,dep,bal;
public:
void init();
void depo();
void with();
void disp();
};
void bank::init()
{
cout<<"Name::";
cin>>name;
cout<<"Enter s for savings or C for current account::";
cin>>type;
}
void bank::depo()
{
cout<<"Account no & amt to deposit::";
cin>>acno>>dep;
bal=0;
bal+dep;
}
void bank::with()
{
cout<<"Amt to withdraw::";
cin>>amt;
if(amt>bal)
cout<<"Insufficient balance";
else
bal-=amt;
}
void bank::disp()
{
cout<<"Name::"<<name<<endl
<<"Balance::"<<bal<<endl;
}
void main()
{
clrscr();
bank b[10];
int ch,i,j;
for(i=1;i<=2;i++)
{
b[i].init();
}
while(1)
{
cout<<"1 to deposit"<<endl
<<"2 to withdraw"<<endl
<<"3 to deposit"<<endl
<<"4 to Exit::";
cin>>ch;
if(ch==1||ch==2||ch==3)
{
cout<<endl<<"Which customer details do you want to enter or view::";
cin>>j;
}
else
exit(1);
getch();
switch(ch)
{
case 1:
b[j].depo();
break;
case 2:
b[j].with();
break;
case 3:
b[j].disp();
break;
case 4:
exit(1);
}
getch();
}
}
FIRST METHOD
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class bank
{
char name[10],type;
int acno;
float amt,dep,bal;
public:
void init();
void depo();
void with();
void disp();
};
void bank::init()
{
cout<<"Name::";
cin>>name;
cout<<"Enter s for savings or C for current account::";
cin>>type;
}
void bank::depo()
{
cout<<"Account no & amt to deposit::";
cin>>acno>>dep;
bal=0;
bal+=dep;
}
void bank::with()
{
cout<<"Amt to withdraw::";
cin>>amt;
if(amt>bal)
cout<<"Insufficient balance";
else
bal-=amt;
}
void bank::disp()
{
cout<<"Name::"<<name<<endl
<<"Balance::"<<bal<<endl;
}
void main()
{
clrscr();
bank b;
b.init();
b.depo();
b.with();
b.disp();
getch();
}
ANOTHER WAY TO SOLVE THE PROBLEM
#include<conio.h>#include<iostream.h>
#include<stdlib.h>
class bank
{
char name[10],type;
int acno;
float amt,dep,bal;
public:
void init();
void depo();
void with();
void disp();
};
void bank::init()
{
cout<<"Name::";
cin>>name;
cout<<"Enter s for savings or C for current account::";
cin>>type;
}
void bank::depo()
{
cout<<"Account no & amt to deposit::";
cin>>acno>>dep;
bal=0;
bal+dep;
}
void bank::with()
{
cout<<"Amt to withdraw::";
cin>>amt;
if(amt>bal)
cout<<"Insufficient balance";
else
bal-=amt;
}
void bank::disp()
{
cout<<"Name::"<<name<<endl
<<"Balance::"<<bal<<endl;
}
void main()
{
clrscr();
bank b[10];
int ch,i,j;
for(i=1;i<=2;i++)
{
b[i].init();
}
while(1)
{
cout<<"1 to deposit"<<endl
<<"2 to withdraw"<<endl
<<"3 to deposit"<<endl
<<"4 to Exit::";
cin>>ch;
if(ch==1||ch==2||ch==3)
{
cout<<endl<<"Which customer details do you want to enter or view::";
cin>>j;
}
else
exit(1);
getch();
switch(ch)
{
case 1:
b[j].depo();
break;
case 2:
b[j].with();
break;
case 3:
b[j].disp();
break;
case 4:
exit(1);
}
getch();
}
}
FUNCTIONS_3
WAP TO PRINT THE LARGEST OF THE 2 NUMBERS USING (?:) CONDITIONAL OPERATOR
#include<iostream.h>
#include<conio.h>
class comparison
{
int x,y,max;
public:
void get();
void largest();
void print();
};
void comparison::get()
{
cout<<"Enter value of x & y::";
cin>>x>>y;
}
void comparison::largest()
{
max=(x>y)?x:y;
}
void comparison::print()
{
cout<<"x="<<x<<endl<<"y="<<y<<endl
<<"max="<<max ;
}
void main()
{
clrscr();
comparison c;
c.get();
c.largest();
c.print();
getch();
}
#include<iostream.h>
#include<conio.h>
class comparison
{
int x,y,max;
public:
void get();
void largest();
void print();
};
void comparison::get()
{
cout<<"Enter value of x & y::";
cin>>x>>y;
}
void comparison::largest()
{
max=(x>y)?x:y;
}
void comparison::print()
{
cout<<"x="<<x<<endl<<"y="<<y<<endl
<<"max="<<max ;
}
void main()
{
clrscr();
comparison c;
c.get();
c.largest();
c.print();
getch();
}
FUNCTIONS_2
WAP TO ENTER NUMBER OF STUDENTS AS PER USER CHOICE AND INPUT MARKS IN SUBJECTS AND DISPLAY
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[30];
float eng,math,sci,tot;
public:
void init();
void caltot();
void display();
};
void student::init()
{
cout<<"Enter rno,name,eng,math & science marks::";
cin>>rno>>name>>eng>>math>>sci;
}
void student::caltot()
{
tot=sci+math+eng;
}
void student::display()
{
cout<<"Rno::"<<rno<<endl
<<"Name::"<<name<<endl
<<"Science::"<<sci<<endl
<<"Math::"<<math<<endl
<<"English::"<<eng<<endl
<<"total::"<<tot<<endl;
}
void main()
{
clrscr();
int i,n;
cin>>n;
student s[4];
for(i=0;i<n;i++)
{
s[i].init();
s[i].caltot();
s[i].display();
}
getch();
}
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[30];
float eng,math,sci,tot;
public:
void init();
void caltot();
void display();
};
void student::init()
{
cout<<"Enter rno,name,eng,math & science marks::";
cin>>rno>>name>>eng>>math>>sci;
}
void student::caltot()
{
tot=sci+math+eng;
}
void student::display()
{
cout<<"Rno::"<<rno<<endl
<<"Name::"<<name<<endl
<<"Science::"<<sci<<endl
<<"Math::"<<math<<endl
<<"English::"<<eng<<endl
<<"total::"<<tot<<endl;
}
void main()
{
clrscr();
int i,n;
cin>>n;
student s[4];
for(i=0;i<n;i++)
{
s[i].init();
s[i].caltot();
s[i].display();
}
getch();
}
FUNCTIONS_1
WAP TO ENTER MARKS IN EACH SUBJECTS AND CALCULATE & DISPLAY THE TOTAL MARKS USING FUNCTION
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[30];
float eng,math,sci,tot;
public:
void init();
void caltot();
void display();
};
void student::init()
{
cout<<"Enter rno,name,eng,math & science marks::";
cin>>rno>>name>>eng>>math>>sci;
}
void student::caltot()
{
tot=sci+math+eng;
}
void student::display()
{
cout<<"Rno::"<<rno<<endl
<<"Name::"<<name<<endl
<<"Science::"<<sci<<endl
<<"Math::"<<math<<endl
<<"English::"<<eng<<endl
<<"total::"<<tot<<endl;
}
void main()
{
clrscr();
student s1,s2;
cout<<"Enter student 1 details::"<<endl;
s1.init();
s1.caltot();
s1.display();
cout<<"Enter student 2 details::"<<endl;
s2.init();
s2.caltot();
s2.display();
getch();
}
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[30];
float eng,math,sci,tot;
public:
void init();
void caltot();
void display();
};
void student::init()
{
cout<<"Enter rno,name,eng,math & science marks::";
cin>>rno>>name>>eng>>math>>sci;
}
void student::caltot()
{
tot=sci+math+eng;
}
void student::display()
{
cout<<"Rno::"<<rno<<endl
<<"Name::"<<name<<endl
<<"Science::"<<sci<<endl
<<"Math::"<<math<<endl
<<"English::"<<eng<<endl
<<"total::"<<tot<<endl;
}
void main()
{
clrscr();
student s1,s2;
cout<<"Enter student 1 details::"<<endl;
s1.init();
s1.caltot();
s1.display();
cout<<"Enter student 2 details::"<<endl;
s2.init();
s2.caltot();
s2.display();
getch();
}
Subscribe to:
Posts (Atom)





