0% found this document useful (1 vote)
894 views24 pages

Homework Week 6 Done

The document contains examples of C++ code using selection structures like if/else statements and switch statements. The code samples calculate bonuses, rates, room assignments, test scores, raises, and seminar fees based on different conditions. Multiple examples are provided to demonstrate using longer and shorter forms of if statements, converting if/else statements to switch statements, and selecting appropriate outputs based on integer and character variable conditions.

Uploaded by

Ahmad Solihuddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
894 views24 pages

Homework Week 6 Done

The document contains examples of C++ code using selection structures like if/else statements and switch statements. The code samples calculate bonuses, rates, room assignments, test scores, raises, and seminar fees based on different conditions. Multiple examples are provided to demonstrate using longer and shorter forms of if statements, converting if/else statements to switch statements, and selecting appropriate outputs based on integer and character variable conditions.

Uploaded by

Ahmad Solihuddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

EXERCISE WEEK 6

1. Write the c++ code for the multiple-alternative selection structure shown in figure 6-35.
First, use the longer form of the I f statement. Then rewrite the code using the shorter form
of the I f statement.
If (sales < = 0)
Bonus = 0
Display “the sales mus be greater than 0”
Else
if (sales <= 1000)
Bonus =sales * 0.15
Else
If (sales <= 5000)
Bonus = sales * 0.20
Else
Bonus = sales * 0.25
End if
End if
End if
Answer
if (sales <= 0)
cout << "the sales must be greater than 0." << endl ;
else if (sales <= 1000)
bonus = sales * 0.15 ;
else if ( sales <= 5000)
bonus = sales * 0.2 ;
else if (sales >= 5001)
bonus = sales * 0.25 ;

#include <iostream>
using namespace std;
int main()

{
double sales = 0.0 ;
double bonus = 0.0 ;

cout << "enter sales: " ;


cin >> sales ;

if (sales <= 0)
cout << "the sales must be greater than 0." << endl ;
else if (sales <= 1000)
bonus = sales * 0.15 ;
else if ( sales <= 5000)
bonus = sales * 0.2 ;
else if (sales >= 5001)
bonus = sales * 0.25 ;
//end if
cout << "bonus is : $ "<< bonus << endl ;
return 0;
}
2. Using the s w I t c h statement, write the C++ code that corresponds to the partial flowchart
shown in figure 6.36. use a char variable named code and a double variable named rate.

code
e
1 2,3,4 5 6,7 other

Rate=0.2 Rate=0.05 Rate=0.1 Rate= 0.15 Rate= -1

Figure 6-36

#include <iostream>
using namespace std;
int main()
{
char code = ' ' ;
double rate = 0.0 ;

cout << "enter the code number: " ;


cin >> rate ;

switch (code)
{
case '1' :
rate = 0.2 ;
break;
case '2' :
case '3' :
case '4' :
rate = 0.05 ;
break ;
case '5' :
rate = 0.1 ;
break ;
case '6' :
case '7' :
rate = 0.15 ;
break ;
default :
rate = -1 ;
}
cout << "CODE : " << code << endl;
cout << "RATE : " << rate << endl;

return 0 ;
}
3. Complete exercise 2, and then change the switch statement to the multiple-alternative form
of the If statement.

#include <iostream>
using namespace std;
int main()
{
char code = ' ' ;
double rate = 0.0 ;

cout << "enter the code (1 / 2 / 3 / 4 / 5 / 6 /7 ): " ;


cin >> code ;

if (code == '1')
rate = 0.2 ;
else if (code=='2'||code=='3'||code=='4')
rate = 0.05 ;
else if (code=='5')
rate = 0.1 ;
else if (code=='6'||code=='7')
rate = 0.15 ;
else
rate = -1 ;

cout << "CODE : " << code << endl;


cout << "RATE : " << rate << endl;

return 0 ;
}
4. Travis is standing in front of two containers : one marked trash and other marked recycle. In
his right hand, he is holding a bag that contains either trash or recyclables. Travis needs to
lift the lid from the appropriate container (if necessary). Then drop the bag in the container,
and then put the lid back on the container. Write an appropriate algorithm, using only the
instructions listed in figure 6-37. (an instructions can be used more than once.)

else
end if
drop the bag of recyclables in the Recycle container
drop the bag of trash in the Trash container
if (the bag contains trash)
if (the lid is on the Recycle container)
if (the lid is on the Trash container)
lift the Recycle container’s lid using your left hand
lift the Trash container’s lid using your left hand
put the lid back on the Recycle container using your left
hand
put the lid back on the Trash container using your left hand
Figure 6-37

Answer
if (the bag contains trash)
if (the lid is on the trash container)
lift the trash container`s lid using your left hand
drop the bag of trash in the trash container
put the lid back on the trash container using your left
hand
else
if (the lid is on the recycle container)
lift the recycle container`s lid using your left hand
drop the bag of recyclables in the recycle container
put the lid back on the recycle container using your
left hand
end if
5. Write the C++ code for a multiple-alternative selection structure that displays one of
four different messages, depending on the number of seminar participants stored in
an int variable named participants. Display the message “Use the Kanton room”
when the number of seminar participants is at least 75. When the number is 40 through
74, display the message “Use the Harris room”. When the number is 10 through 39,
display the message “Use the small conference room”. When the number is less than 10,
display the message “Cancel the seminar”.

#include <iostream>
using namespace std;
int main()
{
int room = 0;

cout << "Enter number of participants: ";


cin >> room;

if (room >= 75)


cout << "Use the Kanton room" ;
else if (room >=40 && room <= 74)
cout << "Use the Harris room " ;
else if (room >=10 && room <= 39)
cout << "Use the small conference room" ;
else if (room <10)
cout << "Cancel the seminar" ;

return 0;

}
6. A program stores test scores in two int variables named myScore and expectedScore.
Write the C++ code to compare the two scores and then display one of the following messages:
“I met my expectations”, “I scored higher than expected”, or “I scored lower than expected”.

#include <iostream>
using namespace std;
int main()
{
int myScore = 0 ;
int expectedScore= 0 ;

cout << "Enter Score: " ;


cin >> myScore ;
cout << "Enter expected score: ";
cin >> expectedScore ;

if (myScore == expectedScore)
cout << "I meet my expections: " ;
else if (myScore >= expectedScore )
cout << "I scored higher than expected" ;
else if (myScore <= expectedScore)
cout << "I scored lower than expected" ;

return 0 ;
}
7. A program uses a char variable named department and two double variables named salary and
raise. The department variable contains one of the following letters (entered in either uppercase
or lowercase): A, B, C. or D. employees in department A and B are receiving a 2% raise.
Employees in department C are receiving a 1.5% raise, and employees in department D are
receiving a 3% raise. Using the switch statement, write the C++ code to calculate the appropriate
raise amount.
#include <iostream>
using namespace std;
int main()
{
char depart = ' ';
double raise = 0.0 ;

cout << "enter employees department : " ;


cin >> depart ;

switch ((toupper)(depart))
{
case 'A':
case 'B':
raise = 0.02 ;
break ;
case 'C' :
raise = 0.015 ;
break ;
case 'D' :
raise = 0.03 ;
break;
default :
cout << "error" << endl ;
break;
}
cout << "raise amount is :" << raise << endl ;
return 0 ;
}
8. A program uses a char variable named membership and an int variable named age. The
membership variable contains one of the following letters (entered in either uppercase or
lowercase) : M or N. the letter M stands for member and the N stands for non-member. The
program should display the appropriate seminar fee, which is based on a person`s membership
status and age. The fee schedule is shown in figure 6-38. Write the C++ code to display the fee.

seminar fee Criteria

$10 Club member less than 65 years old


$5 Club member at least 65 years old
$20 Non-member less than 65 years old
$15 Non-member at least 65 years old

#include <iostream>
using namespace std;
int main()
{
char membership = ' ';
int age = 0 ;
int semFee = 0 ;

cout << "Enter age : " ;


cin >> age ;
cout << "MEMBERSHIP OR NOT : (M for membersip) (N for not) : " ;
cin >> membership ;

if (membership=='M' || membership =='m')

if (age <=65)
semFee = 10 ;
else
semFee = 5 ;

else if (membership=='N' || membership == 'n')

if (age<=65)
semFee=20 ;
else
semFee=15;

cout << "YOUR AGE: " << age<< endl;


cout << "MEMBERSHIP or NOT: " << membership << endl ;
cout << "YOUR SEMINAR FEE : $ " << semFee << endl ;

return 0 ;

}
9. The C++ code in figure 6-39 should display one of the four messages listed in the figure. The
appropriate message is based on the level entered by the user. Correct the errors in the code.

Level Message
1 or 2 Bronze
3 Silver
4 or 5 Gold
other Invalid ID
Int level = 0;
Cout << “level (1through 5) : “ ;
Cin >> level ;
Swith (level)
{
Case 1:
Case 2:
cout << “bronze” ;
case 3:
break ;
cout << “silver “ ;
case 4:
cour << “gold” ;
default:
cout << “invalid ID” ;
} //end switch

#include <iostream>
using namespace std;
int main ()
{
int level = 0 ;
cout << "Enter your level: " ;
cin >> level ;

switch (level)
{
case 1 :
case 2 :
cout <<"BRONZE" ;
break;
case 3 :
cout << "SILVER" ;
break ;
case 4 :
case 5 :
cout << "GOLD" ;
break;
default:
cout <<"invalid ID" ;
break;
}
cout << " IS YOUR LEVEL " << endl ;
return 0 ;
}
10. Figure 6-40 shows a partially completed chart for a program that displays the amount of a
salesperson`s commission. The commission is based on the salesperson`s sales amount, as
indicated in the figure. Complete the selection structure in the algorithm section of the chart.
Also complete the C++ instructions section. After completing the chart, create a new project (if
necessary) named project, and save it in the folder. Enter the C++ instructions into a source file.
Also enter appropriate comments and any additional instructions required by the compiler.
Display the commission in fixed-point notation with two decimal places. Save and run the
program. Test the program using the following sales amount: 15250, 251990, 500670, and -5.

Sales range ($) Commission


0-100,000 2% of the sales amount
100,001-400,000 5% of the sales amount
400,001 and over 9% of the sales amount
IPO chart information C++ instructions
Input
Sales Int sales = 0 ;

Processing
None

Output
Commission Double commission = 0.0 ;

Algorithm Cout << “enter sales” ;


1. Enter sales Cin>> sales ;
2. If (sales < 0) If (sales < 0)
Commission = -1 Commission = -1 ;
Else Else if ( sales <= 100000 )
If (sales <= 10000) Commission = sales*0.02 ;
Commission = sales *0.02 Else if (sales <= 400000)
Else Commission = sales*0.05 ;
If (sales <=400000) Else if (sales >= 400001)
Commission = sales *0.05 Commission = sales * 0.09;
Else
Commission = sales * 0.09

3. If (commission is not -1) If (commission !=-1)


Display commission with 2 decimals Cout << fixed << setprecision (2) ;
Else Cout << commission << endl;
Display “sales cannot be less than 0” Else
End if Cout << “sales cannot be less than” ;
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int sales = 0 ;
double commission = 0.0 ;

cout << "enter sales: $ " ;


cin>> sales ;

if (sales < 0)
commission = -1 ;
else if ( sales <= 100000 )
commission = sales*0.02 ;
else if (sales <= 400000)
commission = sales*0.05 ;
else if (sales >= 400001)
commission = sales * 0.09;
{
if (commission !=-1)
{
cout << fixed << setprecision (2) ;
cout << commission << endl;
}
else
{
cout << "sales cannot be less than 0" ;
}
}
return 0 ;
}
11. Code the algorithm shown in figure 6-41. Use the switch statement to code the multiple-
alternative selection structure. If necessary, create a new project named project, and save it in
the folder. Enter the C++ instructions into a source file named. Also enter appropriate comments
and any additional instructions required by the compiler. Save and run the program. Test the
program using the following codes : 1, 2, 3, and 7.

IPO chart information C++ instructions


Input
Department code (1, 2, or 3) Int deptCode = 0;

Processing
None

Output
Salary Int salary = 0 ;

Algorithm
1. Enter the department code Cout << “enter the department code” ;
2. If (the department code is one of the following) Cin >> deptCode ;
1. Salary = 25000
2. Salary = 30000 switch (deptCode)
3. Salary = 32000 {
4. Display “invalid code” case 1 :
Salary = 0 salary = 25000 ;
End if break ;
3. Display salary case 2 :
salary = 30000 ;
break ;
case 3 :
salary = 32000 ;
break ;
default :
salary = 0 ;
break ;
}
Cout << “Salary is : $ “ << salary <<
endl;

DESK-CHECK
DEPARTMENT CODE SALARY
1 25000
2 30000
3 32000
7 0
#include <iostream>
using namespace std;
int main()
{
int deptCode = 0;
int salary = 0 ;

cout << "enter the department code: " ;


cin >> deptCode ;

switch (deptCode)
{
case 1 :
salary = 25000 ;
break ;
case 2 :
salary = 30000 ;
break ;
case 3 :
salary = 32000 ;
break ;
default :
salary = 0 ;
break ;
}
cout << "Salary is : $ " << salary << endl;

}
12. Complete exercise 11. If necessary, create a new project named, and save it in the folder. Enter
(or copy) the instructions form the file into a new source file. Be sure to change the filename in
the first comment. Remove the default clause from the switch statements. Modify the code to
verify that the department code is 1, 2, or 3 only. If the department code is valid, use the switch
statement to determine the salary. And then display the salary. If the department code is not
valid, use the switch statement to determine the salary, and then display the salary. If the
department code is not valid, display the “invalid code” message. Save and run the program. Test
the program using the following codes : 1, 2, 3, and 7.

IPO chart information C++ instructions


Input
Department code (1, 2, or 3) Int deptCode = 0;

Processing
None

Output
Salary Int salary = 0 ;

Algorithm
1. Enter the department code Cout << “enter the department code” ;
2. If (the department code is one of the following) Cin >> deptCode ;
Salary = 25000
Salary = 30000 If (deptCode = 1 )
Salary = 32000 Salary = 25000 ;
Display “invalid code” Else if (deptCode = 2)
Salary = 0 Salary = 30000 ;
End if Else if (deptCode = 3)
3. Display salary Salary = 32000 ;
Else if ;
Cout << “invalid code” ;

Cout << “Salary is : $ “ << salary <<


endl;

DESK-CHECK
DEPARTMENT CODE SALARY
1 25000
2 30000
3 32000
7 Invalid code
#include <iostream>
using namespace std;
int main()
{
int deptCode = 0;
int salary = 0 ;

cout << "enter the department code: " ;


cin >> deptCode ;

if (deptCode == 1 )
salary = 25000 ;
else if (deptCode == 2)
salary = 30000 ;
else if (deptCode == 3)
salary = 32000 ;
else
salary = 0 ;
cout << "invalid code" << endl ;

cout << "Salary is : $ " << salary << endl;

}
13. Kariton learning wants a program that displays the amount of money a company owes for a
seminar. The fee per person is based on the number of people the company registers, as shown
in figure 6-42. For example, if the company registers seven people, the total amount owed is
$700. If the user enters a number that is less than or equal to 0, the program should display an
appropriate error message.

Number of registrants Fee per person

1 through 5 $125
6 through 20 $100
21 or more $ 75

a. Create an IPO chart for the problem and then desk-check the algorithm five times, using the
number 4, 8, 22, 0 and -2 as the number of people registered.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the one
shown earlier in figure 6-27. Then code the algorithm into a program.
c. Desk-check the program using the same data used to desk-check the algorithm.

IPO chart information C++ instructions


Input
Number of registrants Int numRegis = 0 ;

Processing
None

Output
Fee per person Int perPerson = 0 ;
Total amount owed Double totalAmount = 0.0 ;

Algorithm
1. Enter number of registrants Cout << “enter number of registrants” ;
2. If Cin >> numRegis ;
1 through 5
Fee per person = $125 If (numRegis >= 1 *&& numRegis <=5)
Else perPerson = 125 ;
If 6 through 20 else if (numRegis >= 6 *&& numRegis <=20)
Fee per person = $100 perPerson = 100;
Else else if (numRegis >= 21)
If 21 or more perPerson = 75
Fee per person = $75 else if ( numRegis <=0)
Else cout << “appropriate error message” << endl ;
Display appropriate error message
3. Calculated total amount owed by
multiplying number of registrants by totalAmount = numRegis * perPerson ;
fee per person

4. Display fee per person and total cout << “fee per person is : $ “ << perPerson << endl;
amount owed cout << "total amount owed is: $ "<< totalAmount <<
endl;
#include <iostream>
using namespace std;
int main ()
{
int numRegis = 0 ;
int perPerson = 0 ;
double totalAmount = 0.0;
cout << "enter number of registrants : " ;
cin >> numRegis ;

if (numRegis >= 1 && numRegis <=5)


perPerson = 125 ;
else if (numRegis >= 6 && numRegis <=20)
perPerson = 100;
else if (numRegis >= 21)
perPerson = 75 ;
else
cout << "appropriate error message" << endl ;

totalAmount = numRegis * perPerson ;

cout << "fee per person is : $ " << perPerson << endl;
cout << "total amount owed is: $ "<< totalAmount << endl;

return 0 ;
}

NUMBER REGISTRATION FEE PER PERSON TOTAL AMOUNT OWED


4 125 500
8 100 800
22 75 1650
0 Appropriate error message 0
-2 Appropriate error message 0
14. The owner of Harry`s Car Sales pays each salesperson a commission based on his or her quarterly
sales. The sales ranges and corresponding commission rates are shown in figure 6-43. The
program should display an error message if the sales amount is less than 0.
Quartely sales ($) Commission

0-20,000 Multiply the sales by 5%


20,001-50,000 Multiply the sales over 20,000 by 7% and then add
1000 to the result
50,001 or more Multiply the sales over 50,000 by 10% and then add
3,100 to the result

a. Create an IPO chart for the problem, and then desk-check the algorithm seven times, using
sales of 20000, 20001, 30000, 50000, 50001, 75000 and -3.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the
one shown earlier in figure 6-27. Then code the algorithm into a program .
c. Desk-check the program using the same data used to desk-check the algorithm.

IPO chart information C++ instructions


Input
Quartely Sales Int sales= 0;

Processing
None

Output
Commission Double commission = 0.0;

Algorithm
1. Enter number of quarterly sales Cout<< “enter number of quarterly sales”;
2. If quarterly sales <=20000 Cin >> sales;
Calculated commission by multiplying if (sales>0&&sales<=20000)
sales by 0.05. commission = (sales*0.05);
Else if else if (sales>=20001 && sales
Sales more than 20001 less than <=50000)
50000. commission= (sales*0.07)+1000;
Calculated commission by multiplying else if (sales>=50001)
sales by 0.07 and adding 1000 commission=(sales*0.1)+3100;
Else else
Sales more 50001 cout << "error";
Calculated commission by multiplying
sales by 0.10 and adding 3100.
3. Display commission. cout << “commission is: “ << commission << endl ;

QUARTELY SALES COMMISSION


20000 1000
20001 2400.07
30000 3100
50000 4500
50001 8100.10
75000 10600
-3 error
#include <iostream>
using namespace std;
int main()
{
int sales=0;
double commission = 0.0;

cout << "enter quarterly sales: ";


cin>>sales ;

if (sales>=0&&sales<=20000)
commission = (sales*0.05);
else if (sales>=20001 && sales <=50000)
commission= (sales*0.07)+1000;
else if (sales>=50001)
commission=(sales*0.1)+3100;
else
cout << "error";

cout<< "Commission is: " <<commission<<endl;


return 0;

}
15. In this exercise, you will create a program that displays the number of daily calories needed to
maintain your current weight. The number of calories is based on your gender, activity level, and
weight. The formulas for calculating the daily calories are shown in figure 6-44.

Moderately active female : total daily calories = weight multiplied by 12 calories per pound
Relatively inactive female: total daily calories = weight multiplied by 10 calories per pound
Moderately active male: total daily calories = weight multiplied by 15 calories per pound
Relatively inactive male: total daily calories = weight multiplied by 13 calories per pound
Gender Activity Weight
F I 150
F A 120
M I 180
M A 200

a. Create an IPO chart for the problem, and then desk-check the algorithm using the test data
included in figure 6-44. Also desk-check it using invalid data, such as X as the gender code, K as
the activity code, or a negative number for the weight.
b. List the input, processing, and output items, as well as the algorithm, in a chart similar to the one
shown earlier in figure 6-27. Then code the algorithm into a program
c. Desk-check the program using the same data used to desk-check the algorithm.

IPO chart information C++ instructions


Input
Weight int weight = 0;
gender char gender = ' ';
activity char activity= ' ';
Processing double calories= 0.0;
None
cout <<"enter your weight:" ;
Output cin >> weight ;
Total calories cout << "enter your gender: (M)ale and
Algorithm (F)emale: " ;
1. Enter weight, gender and your activity cin >> gender;
level cout << "Enter yout activity level
2. If gender is female (M)oderately and (R)elative activity : " ;
If moderately active female cin >> activity ;
Calculated total calories by multiplied
weight by 12 calories per pound if (gender=='F'||gender=='f')
Else relative inactive female if (activity=='M'||activity=='m')
Calculated total calories by multiplied calories = weight *12 ;
weight by 10 calories per pound else if
3. Else if gender is male (activity=='R'||activity=='r')
If moderately active male calories = weight *10;
Calculated total calories by multiplied else if (gender=='M'||gender=='m')
weight by 15 calories per pound if (activity=='M'||activity=='m')
Else if relactive inactive male calories = weight *15 ;
Calculated total calories by multiplied else if
weight by 13 calories per pound (activity=='R'||activity=='r')
4. Display total calories calories = weight *13;

cout <<"Your total daily calories is: " <<calories


<< endl;
#include <iostream>
using namespace std;
int main()
{
int weight = 0;
char gender = ' ';
char activity= ' ';
double calories= 0.0;

cout <<"enter your weight:" ;


cin >> weight ;
cout << "enter your gender: (M)ale and (F)emale: " ;
cin >> gender;
cout << "Enter yout activity level (M)oderately and (R)elative activity : " ;
cin >> activity ;

if (gender=='F'||gender=='f')
{
if (activity=='M'||activity=='m')
calories = weight *12 ;
else if (activity=='R'||activity=='r')
calories = weight *10;
}
else if (gender=='M'||gender=='m')
{
if (activity=='M'||activity=='m')
calories = weight *15 ;
else if (activity=='R'||activity=='r')
calories = weight *13;
}

cout <<"Your total daily calories is: " <<calories << endl;

return 0;

}
16. In this exercise, you will create a program that displays both the smallest and largest of three
integers entered by the user. For example, if the user enters the numbers 3, 5, and 9, the
program should display the messages “Smallest number is 3”. And “largest number is 9” on the
computer screen.
a. Create an IPO chart for the problem, and then desk-check the algorithm four times. For the
first desk-check, use the number 3, 5, and 9. For the second desk-check , use 7, 10, and 12.
For the third desk-check use 8,4, and 6. For the fourth desk-check , use 1, 9 and 1.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the
one shown earlier in figure 6-27. Then code the algorithm into a program.
c. Desk-check the program using the same data used to desk-check the algorithm.

IPO chart information C++ instructions


Input
First number int a= 0;
Second number int b = 0;
Third number int c=0;
int smallest = 0;
Processing int largest = 0;
None
cout << "input first number: ";
Output cin >> a;
Smallest number cout << "input second number: ";
Largest number cin >> b;
Algorithm cout <<"input third number: ";
1. Enter first , second and third number. cin>> c;
2. If if (a<b&&a<c)
Number greatest than other smallest=a;
Display largest number else if (b<a&b<c)
Else smallest=b;
Display smallest number else if (c<a&&c<b)
3. End smallest = c;
if (a>b&&a>c)
largest=a;
else if (b>a&&b>c)
largest = b;
else if (c>a&&c>b)
largest = c;
cout<< largest << " is the largest" <<
endl;
cout << smallest <<" is the smallest" << endl;

First number Second number Third number Smallest Largest number


number
3 5 9 3 9
7 10 12 7 12
8 4 6 4 8
#include <iostream>
using namespace std;
int main()
{
int a= 0;
int b = 0;
int c=0;
int smallest = 0;
int largest = 0;

cout << "input first number: ";


cin >> a;
cout << "input second number: ";
cin >> b;
cout <<"input third number: ";
cin>> c;

{
if (a<b&&a<c)
smallest=a;
else if (b<a&b<c)
smallest=b;
else if (c<a&&c<b)
smallest = c;
}
{
if (a>b&&a>c)
largest=a;
else if (b>a&&b>c)
largest = b;
else if (c>a&&c>b)
largest = c;
}
cout<< largest << " is the largest" << endl;
cout << smallest <<" is the smallest" << endl;

return 0;
}

You might also like