Write a program that will ask the user to input an integer then output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the inputted number is not within 1-7, output "Day is not available!"
Solution:
#include <iostream>
using namespace std;
int main()
{
int day;
cout<<"Enter a number from 1 to 7: ";
cin>>day;
switch (day)
{
case 1:
cout<<"Sunday"<<endl;
break;
case 2:
cout<<"Monday"<<endl;
break;
case 3:
cout<<"Tuesday"<<endl;
break;
case 4:
cout<<"Wednesday"<<endl;
break;
case 5:
cout<<"Thursday"<<endl;
break;
case 6:
cout<<"Friday"<<endl;
break;
case 7:
cout<<"Saturday"<<endl;
break;
default:
cout<<"Day is not available!"<<endl;
}
}
IT Teaching Resources
Sunday, December 15, 2013
C++ Programming: Salary
Write a program that asks the user for the hours worked for the week and the hourly rate. The basic salary is computed as:
salary = hours worked * hourly rate
Bonuses are given:
No. of hours > 45 | Bonus of 500 pesos
No. of hours > 40 and <=45 | Bonus of 250 pesos
No. of hours > 35 and <=40 | Bonus of 150 pesos
Display the basic salary, bonus and the total salary (basic salary + bonus) for the week.
Solution:
#include <iostream>
using namespace std;
int main()
{
float amount, hours, bonus, total;
cout<<"Enter hours worked: ";
cin>>hours;
cout<<"Enter the hourly rate: ";
cin>>amount;
if (hours >45)
bonus = 500;
else if (hours > 40 && hours <=45)
bonus = 250;
else if (hours >35 && hours <=40)
bonus = 150;
else
bonus = 0;
total= (hours*amount) + bonus;
cout<<"The basic salary is "<< hours*amount<<endl;
cout<<"The bonus is "<<bonus<<endl;
cout<<"The total salary is "<<total<<endl;
return 0;
}
salary = hours worked * hourly rate
Bonuses are given:
No. of hours > 45 | Bonus of 500 pesos
No. of hours > 40 and <=45 | Bonus of 250 pesos
No. of hours > 35 and <=40 | Bonus of 150 pesos
Display the basic salary, bonus and the total salary (basic salary + bonus) for the week.
Solution:
#include <iostream>
using namespace std;
int main()
{
float amount, hours, bonus, total;
cout<<"Enter hours worked: ";
cin>>hours;
cout<<"Enter the hourly rate: ";
cin>>amount;
if (hours >45)
bonus = 500;
else if (hours > 40 && hours <=45)
bonus = 250;
else if (hours >35 && hours <=40)
bonus = 150;
else
bonus = 0;
total= (hours*amount) + bonus;
cout<<"The basic salary is "<< hours*amount<<endl;
cout<<"The bonus is "<<bonus<<endl;
cout<<"The total salary is "<<total<<endl;
return 0;
}
C++ Programming: Smallest Number
Problem: Write a C++ program that finds the smallest among the five integers inputted by the user.
Solution #1:
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e,smallest;
cout<<"Enter five numbers \n";
cout<<"Number 1: ";
cin>>a;
cout<<"Number 2: ";
cin>>b;
cout<<"Number 3: ";
cin>>c;
cout<<"Number 4: ";
cin>>d;
cout<<"Number 5: ";
cin>>e;
smallest = a;
if (smallest >b)
smallest = b;
if (smallest >c)
smallest = c;
if (smallest >d)
smallest = d;
if (smallest >e)
smallest = e;
cout<<"The smallest number is "<<smallest<<endl;
return 0;
}
Solution #2
Solution #1:
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e,smallest;
cout<<"Enter five numbers \n";
cout<<"Number 1: ";
cin>>a;
cout<<"Number 2: ";
cin>>b;
cout<<"Number 3: ";
cin>>c;
cout<<"Number 4: ";
cin>>d;
cout<<"Number 5: ";
cin>>e;
smallest = a;
if (smallest >b)
smallest = b;
if (smallest >c)
smallest = c;
if (smallest >d)
smallest = d;
if (smallest >e)
smallest = e;
cout<<"The smallest number is "<<smallest<<endl;
return 0;
}
Solution #2
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"Enter five numbers \n";
cout<<"Number 1: ";
cin>>a;
cout<<"Number 2: ";
cin>>b;
cout<<"Number 3: ";
cin>>c;
cout<<"Number 4: ";
cin>>d;
cout<<"Number 5: ";
cin>>e;
if (a<b && a<c && a<d && a<e)
cout<<"The smallest number is "<<a<<endl;
else if (b<a && b<c && b<d && b<e)
cout<<"The smallest number is "<<b<<endl;
else if (c<a && c<b && c<d && c<e)
cout<<"The smallest number is "<<c<<endl;
else if (d<a && d<c && d<c && d<e)
cout<<"The smallest number is "<<d<<endl;
else
cout<<"The smallest number is "<<e<<endl;
return 0;
}
Subscribe to:
Posts (Atom)