QUESTION 1
1 2 4 5 6
The final value of x is: 7
QUESTION 2
int main()
{
cout << "\t\t\t\t ***Multiplication Table***" << endl;
cout << "\t 1\t 2 \t 3 \t 4 \t 5 \t 6 \t 7 \t 8 \t 9 \t 10" << endl;
for (int i=1; i<=10; i++){
cout << i;
for (int b=1; b<=10; b++){
int newNumber = i*b;
if (b==10){
cout << " \t " << newNumber << endl;
}else{
cout << " \t " << newNumber;
}
}
}
return 0;
}
QUESTION 3
a)
int main()
{
int height = 9;
for (int i=height; i >= -height; i-=2){
if (i==height || i==-height){
for (int i=1; i <= height; i++){
cout << "#";
}
cout << endl;
}else{
int abs_i;
if (i < 0){
abs_i = i*-1;
}else{
abs_i = i;
}
cout << "#";
int space_count = (7 - abs_i)/2;
for (int c_1=0; c_1<space_count; c_1++){
cout << " ";
}
for (int d=0; d<abs_i; d++){
cout << "*";
}
for (int c_2=0; c_2<space_count; c_2++){
cout << " ";
}
cout << "#";
cout << endl;
}
}
return 0;
}
b)
int main()
{
int height=0, checkpoint=0;
while (checkpoint < 1){
cout << "Enter the height of the egg timer: ";
cin >> height;
if (height < 5 || height % 2 == 0){
cout << "Invalid number! Please enter an odd number greater than 3!" <<
endl;
}else{
checkpoint = 10;
}
}
for (int i=height; i >= -height; i-=2){
if (i==height || i==-height){
for (int i=1; i <= height; i++){
cout << "#";
}
cout << endl;
}else{
int abs_i;
if (i < 0){
abs_i = i*-1;
}else{
abs_i = i;
}
cout << "#";
int space_count = (height - 2 - abs_i)/2;
for (int c_1=0; c_1<space_count; c_1++){
cout << " ";
}
for (int d=0; d<abs_i; d++){
cout << "*";
}
for (int c_2=0; c_2<space_count; c_2++){
cout << " ";
}
cout << "#";
cout << endl;
}
}
return 0;
}
QUESTION 4
int main()
{
int total = 0;
for (int i=1; i<=500; i++){
int quotients = 0;
for (int d=1; d<=i; d++){
if (i%d == 0 && d != i && d != 1){
quotients++;
}
}
if(quotients == 0){
cout << i << " ";
total+=i;
}
}
cout << endl << "The total is: " << total;
return 0;
}
QUESTION 5
int main()
{
cout << setw(12) << left << "n" << right << "pi" << endl;
for (int i=10; i<=500; i+=10){
double initial_num = 0;
int counter = 1;
int starting_denom = 1;
while (counter <= i){
if (counter % 2 == 0){
initial_num = initial_num - 4.0/starting_denom;
}else{
initial_num = initial_num + 4.0/starting_denom;
}
starting_denom +=2;
counter++;
}
cout << setw(12) << left << i << right << fixed << setprecision(5) <<
initial_num << endl;
}
return 0;
}
QUESTION 6
int main()
{
double income = 0, tax, taxed_income;
int taxed_bracket;
while (income >= 0){
cout << "Please enter your monthly income: ";
cin >> income;
taxed_income = income;
while (taxed_income > 0){
taxed_bracket+=5000;
taxed_income-=5000;
taxed_bracket+=15000;
taxed_income-=15000;
tax = 15000*0.01;
taxed_bracket+=15000;
taxed_income-=15000;
tax = tax + 15000*0.03;
taxed_bracket+=6969;
switch(taxed_bracket){
case 5000:
tax = 0;
break;
case 20000:
tax = tax + (income-5000)*0.01;
break;
case 35000:
tax = tax + (income-20000)*0.03;
break;
default:
tax = tax + (income-35000)*0.06;
}
cout << "Your total income tax is RM " << fixed << setprecision(2) << tax
<< endl;
return 0;
}