Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #include<conio.h>
- int jar1=0,jar2=0;
- void fill_jar1();
- void fill_jar2();
- void jar1_jar2();
- void jar2_jar1();
- void empty_jar1();
- void empty_jar2();
- void show_jars();
- void main()
- {
- int ch;
- clrscr();
- cout<<"capacity of jar 1 is 4 litre"<<endl;
- cout<<"capacity of jar 2 is 3 litre";
- do
- {
- cout<<"\n"<<endl;
- cout<<"1.fill jar"<<endl;
- cout<<"2.empty jar"<<endl;
- cout<<"3.pour from jar 1 to jar 2"<<endl;
- cout<<"4.pour from jar 2 to jar 1"<<endl;
- cout<<"5.show jars"<<endl;
- cout<<"enter choice:";
- cin>>ch;
- switch(ch)
- {
- case 1:int x;
- cout<<"\nenter 1 to fill jar 1"<<endl;
- cout<<"enter 2 to fill jar 2"<<endl;
- cout<<"enter choice:";
- cin>>x;
- if(x==1)
- fill_jar1();
- else if(x==2)
- fill_jar2();
- break;
- case 2:int y;
- cout<<"\nenter 1 to empty jar 1"<<endl;
- cout<<"enter 2 to empty jar 2"<<endl;
- cout<<"enter choice:";
- cin>>y;
- if(y==1)
- empty_jar1();
- else if(y==2)
- empty_jar2();
- break;
- case 3:jar1_jar2();
- break;
- case 4:jar2_jar1();
- break;
- case 5:show_jars();
- break;
- default:break;
- }
- }
- while((jar1!=2)&&(jar2<=3));
- cout<<"\nJar 1 contains 2 litres"<<endl;
- cout<<"problem solved"<<endl;
- getch();
- }
- void fill_jar1()
- {
- jar1=4;
- }
- void fill_jar2()
- {
- jar2=3;
- }
- void jar1_jar2()
- {
- if((jar1==0)&&(jar2==0))
- cout<<"both jars are empty:";
- if((jar1==4)&&(jar2==3))
- cout<<"both jars are full:";
- while((jar1!=0)&&(jar2!=3))
- {
- jar1--;
- jar2++;
- }
- if(jar2==3)
- cout<<"jar2 is full";
- }
- void jar2_jar1()
- {
- if((jar1==0 )&&(jar2==0))
- cout<<"jars are empty:";
- if((jar1==4)&&(jar2==3))
- cout<<"both jars are full:";
- while((jar1!=4)&&(jar2!=0))
- {
- jar1++;
- jar2--;
- }
- if(jar1==4)
- cout<<"jar1 is full";
- }
- void empty_jar1()
- {
- jar1=0;
- cout<<"jar 1 is empty";
- }
- void empty_jar2()
- {
- jar2=0;
- cout<<"jar 2 is empty";
- }
- void show_jars()
- {
- cout<<"\n"<<endl;
- cout<<"| |"<<"\t"<<"| |"<<endl;
- cout<<"| "<<jar1<<" |"<<"\t"<<"| "<<jar2<<" |"<<endl;
- cout<<"|_____|"<<"\t"<<"|_____|"<<endl;
- cout<<" jar 1"<<"\t"<<" jar 2 "<<endl;
- }
- output:
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 0 | | 0 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:1
- enter 1 to fill jar 1
- enter 2 to fill jar 2
- enter choice:1
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 4 | | 0 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:1
- enter 1 to fill jar 1
- enter 2 to fill jar 2
- enter choice:2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 4 | | 3 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:2
- enter 1 to empty jar 1
- enter 2 to empty jar 2
- enter choice:1
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 0 | | 3 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:4
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 3 | | 0 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:1
- enter 1 to fill jar 1
- enter 2 to fill jar 2
- enter choice:2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 3 | | 3 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:4
- jar1 is full
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 4 | | 2 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:2
- enter 1 to empty jar 1
- enter 2 to empty jar 2
- enter choice:1
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 0 | | 2 |
- |_____| |_____|
- jar 1 jar 2
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:4
- 1.fill jar
- 2.empty jar
- 3.pour from jar 1 to jar 2
- 4.pour from jar 2 to jar 1
- 5.show jars
- enter choice:5
- | | | |
- | 2 | | 0 |
- |_____| |_____|
- jar 1 jar 2
- Jar 1 contains 2 litres
- problem solved
Advertisement
Add Comment
Please, Sign In to add comment