# EXERCISE IN CLASS

Document Sample

```					EXERCISE IN CLASS
CHAPTER 5
PART 1
STRUCTURED PROGRAMMING

SELECTION
SCENARIO 1

Using if statement, write a
program for following scenario:
• Ask user to enter weight.
Program will only displays
status “You’re obese” if the
weight is more than 100kg.
SCENARIO 3
Modify question in scenario 1.
Program will displays status
based on below condition:
• weight >100kg, status=“Obese”
• weight < 30kg, status=
“Underweight”
• others, status = “Normal”
SCENARIO 4
Using switch statements, writ a C program for below scenario:
Ask the user to enter room code and number
of days they want to stay. Identify the room
type and room price based on the table given
below:
Code      Type          Price (RM)
•    D      Deluxe           200.00
•    T       Twin Sharing 170.00
•    S        Single          120.00
Display details of info to user.
SCENARIO 5
Convert the following if-else statements to switch statements.
if (input == ‘M’ || input == ‘m’) {
printf(“Gender : Male \n”);
printf(“You can’t join party tonite\n”);
}
else if (input == ‘F’ || input == ‘f’) {
printf(“Gender : Female \n”);
printf(“Join the party tonite!\n”);
}
else
printf(“Invalid. You’re not human!”);
SCENARIO 6
Trace the output of the following code segments:

int no = 3;
switch (no) {
case 1 : no++;
case 3 : no = 6;
case 4 : no * = 2;
default : - - no;
}
printf(“No is %d ” , no);
PART 2
STRUCTURED PROGRAMMING

REPETITION
SCENARIO 1
Hint : Use a while loop.

• Ask user to enter an alphabet. Display
the alphabet up to 5 times.

• Display multiplication table for no 5 up to
10 values. Sample output:
5 10 15 20 25 30 35 40 45 50
SCENARIO 2
Hint : Use a do-while loop.

Prompt user to enter an alphabet.
Program will displays the alphabet
according to how many times user wants
the alphabet to be displayed.
SCENARIO 3
Hint : Use for loop.

Ask user to enter 5 numbers. Program
will displays the number only if the
number is a positive no.
SCENARIO 4
1. Convert the following while statement to for and do-while.
2. Trace the output.

int value = 0, round = 5;
while (value +1 <= --round) {
printf(“Tension\n”);
value ++ ;
}
printf(“Migraine!”);
THAT’S ALL FOR
TODAY……

SEE YA NEXT
CLASS 

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 2 posted: 9/15/2012 language: English pages: 13
How are you planning on using Docstoc?