###### tags: `C++ Lab` # **Chapter 2 (Condition)** 1. Check odd/even ``` #include <iostream> using namespace std; int main() { int num; cout << "Please input an integer number: "; cin >> num; if(num%2==0) { cout << num << " is an even number"; } else { cout << num << " is an odd number"; } return 0; } ``` 2. average, pass/fail ``` #include <iostream> using namespace std; int main() { double s1,s2,s3,s4,s5,avg; cout << "Please enter 5 scores: "; cin >> s1 >> s2 >> s3 >> s4 >> s5; avg = (s1+s2+s3+s4+s5)/5; if(avg > 4.0) { cout << "You got the final score of " << avg << ". " << "Congas!! You passed the course."; } else { cout << "You got the final score of " << avg << ". " << "Oh no!! You failed the course."; } return 0; } ``` 3. Print Grade ``` #include <iostream> using namespace std; int main(){ double s1,s2,s3,s4,s5,avg; cout << "Please enter 5 scores: "; cin >> s1 >> s2 >> s3 >> s4 >> s5; avg = (s1+s2+s3+s4+s5)/5; if (avg<4.0) { cout << "You got the final score of " << avg << ". " << "Your letter grade is F."; } else if (avg<5.0){ cout << "You got the final score of " << avg << ". " << "Your letter grade is D."; } else if (avg<6.0){ cout << "You got the final score of " << avg << ". " << "Your letter grade is D+."; } else if (avg<7.0){ cout << "You got the final score of " << avg << ". " << "Your letter grade is C+."; } else if (avg<8.0){ cout << "You got the final score of " << avg << ". " << "Your letter grade is B."; } else if (avg<9.0){ cout << "You got the final score of " << avg << ". " << "Your letter grade is B+."; } else { cout << "You got the final score of " << avg << ". " << "Your letter grade is A."; } return 0; } ``` 4. Solve ax+b=0 ``` #include <iostream> using namespace std; int main() { int x,a,b; cout << "Input 2 real numbers a and b: "; cin >> a >> b; if (a!=0) { x = -b/a; cout << "The unique solution of x is: " << x; } else if (b!=0) { cout << "Inf solutions"; } else { cout << "Infinite numbers of solution"; } return 0; } ``` 5. Solve ax2+bx+c=0 ``` #include <iostream> #include <cmath> using namespace std; int main() { double a,b,c,d; cout << "Input 3 real numbers a, b and c: "; cin >> a >> b >> c; if (a==0) { if (b==0) if (c==0) cout << "Infinite numbers of solution"; else cout << "No solution"; else cout << "Unique solution x=-b/a = " << -b/a; } else { d = pow(b,2)-4*a*c; if (d>0) { cout << "Two solution: " << endl; cout << "x1 = " << (-b+sqrt(pow(b,2)-4*a*c))/(2*a) << endl; cout << "x2 = " << (-b-sqrt(pow(b,2)-4*a*c))/(2*a) << endl; } else if (d==0) { cout << "One solution: -b/2a = " << -b/(2*a); } else cout << "Complex solution"; } return 0; ``` } 6. Print the number of days in the month and year ``` #include <iostream> using namespace std; int main() { int m,y,days; cout << "Input the month and year of a date: "; cin >> m >> y; switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if (y%100==0 || (y%4==0 && y%100 != 0)) { days = 29; } else { days =28; } break; } cout << "This month has " << days << " days"; return 0; ``` } 7. Check valid date ``` #include <iostream> using namespace std; int main() { int d,m,y,days; cout << "Input the date: "; cin >>d>>m>>y; switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if (y%100==0 || (y%4==0 && y%100 != 0)) { days = 29; } else { days =28; } break; } (d<=days&&d>=1) ? cout << "YES": cout << "NO"; } ``` 8. Calculate the next dd/mm/yyyy of that day ``` #include <iostream> using namespace std; int main() { int d,m,y; cout << "Input the date: "; cin>>d>>m>>y; if (d > 0 && d < 28) { d+=1; } if (d == 28) { if (m == 2) { if (y%100==0 || (y%4==0 && y%100 != 0)) { d = 29; } else { d = 1; m = 3; } } else { d += 1; } } if (d == 29 && m==2) { d=1; m=3; } else if(d==29) { d+=1; } if (d == 30) { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { d += 1; } else { d = 1; m += 1; } } if (d == 31) { d = 1; if (m == 12) { y += 1; m = 1; } else { m += 1; } } if (d < 10) { cout<<"0"<<d<<"/"; } else { cout<<d<<"/"; } if (m < 10) { cout << "0" << m << "/"; } else { cout << m << "/"; } cout << y; return 0; } ``` 9. Calculate how many day of the year ``` #include <iostream> using namespace std; int main(){ int d,m,y,day; cout<<"Input the date: "; cin>>d>>m>>y; if (y%100==0 || (y%4==0 && y%100 != 0)) { switch(m) { case 1: day=d; break; case 2: day=31+d; break; case 3: day=31+29+d; break; case 4: day=31+29+31+d; break; case 5: day=31+29+31+30+d; break; case 6: day=31+29+31+30+31+d; break; case 7: day=31+29+31+30+31+30+d; break; case 8: day=31+29+31+30+31+30+31+d; break; case 9: day=31+29+31+30+31+30+31+31+d; break; case 10: day=31+29+31+30+31+30+31+31+30+d; break; case 11: day=31+29+31+30+31+30+31+31+30+31+d; break; case 12: day=31+29+31+30+31+30+31+31+30+31+30+d; break; } } else { switch(m) { case 1: day=d; break; case 2: day=31+d; break; case 3: day=31+28+d; break; case 4: day=31+28+31+d; break; case 5: day=31+28+31+30+d; break; case 6: day=31+28+31+30+31+d; break; case 7: day=31+28+31+30+31+30+d; break; case 8: day=31+28+31+30+31+30+31+d; break; case 9: day=31+28+31+30+31+30+31+31+d; break; case 10: day=31+28+31+30+31+30+31+31+30+d; break; case 11: day=31+28+31+30+31+30+31+31+30+31+d; break; case 12: day=31+28+31+30+31+30+31+31+30+31+30+d; break; } } cout<<"The day of the year is "<<day; return 0; } ``` 10. sort 3 numbers (asc) (NOT recommend) ``` #include <iostream> using namespace std; int main() { int a,b,c,temp; cout << "Input 3 numbers a, b, and c: "; cin >> a >> b >> c; if(a>b) { temp=a; a=b; b=temp; } if(b>c) { temp=b; b=c; c=temp; } if (a>c) { temp=a; a=c; c=temp; } cout << a << " " << b << " " << c; } ``` 11. Convert 2-digit number to word ``` #include <iostream> using namespace std; int main() { int num,l,r; cin >> num; l=num/10; r=num%10; if(num>=10 && num<=19) { switch(num) { case 10: cout<<"ten"; break; case 11: cout<<"eleven"; break; case 12: cout<<"twelve"; break; case 13: cout<<"thirteen"; break; case 14: cout<<"fourteen"; break; case 15: cout<<"fifteen"; break; case 16: cout<<"sixteen"; break; case 17: cout<<"seventeen"; break; case 18: cout<<"eighteen"; break; case 19: cout<<"nineteen"; break; } } else if(l>=2 && l<=9) { switch(l) { case 2: cout<<"twenty"; break; case 3: cout<<"thirty"; break; case 4: cout<<"forty"; break; case 5: cout<<"fifty"; break; case 6: cout<<"sixty"; break; case 7: cout<<"seventy"; break; case 8: cout<<"eighty"; break; case 9: cout<<"ninety"; break; } if(r>=1) { switch(r) { case 1: cout<<"-one"; break; case 2: cout<<"-two"; break; case 3: cout<<"-three"; break; case 4: cout<<"-four"; break; case 5: cout<<"-five"; break; case 6: cout<<"-six"; break; case 7: cout<<"-seven"; break; case 8: cout<<"-eight"; break; case 9: cout<<"-nine"; break; } } } return 0; } ``` 12. Calculate electricity bill ``` #include <iostream> using namespace std; int main() { double o,n, kwh, price; cout << "Input the old and new indicators: "; cin >> o >> n; kwh = n-o; if (kwh<120) { price=kwh*1.63; } else if (kwh<330) { price=120*1.63 + (kwh-120)*2.38; } else if (kwh<500) { price=120*1.63+210*2.38+(kwh-330)*3.52; } else if (kwh<700) { price=120*1.63+210*2.38+170*3.52+(kwh-500)*4.8; } else if (kwh<1000) { price=120*1.63+210*2.38+170*3.52+700*5.66+(kwh-700)*5.66; } else if (kwh>1000) { price=120*1.63+210*2.38+170*3.52+700*5.66+1000*5.66+(kwh-1000)*6.99; } cout << "kWh used: " << kwh << endl; cout << "Price: " << price << endl; return 0; } ```