Professional Documents
Culture Documents
BERC 1313 Lab 2 SELECTION CONTROL TECHNIQUE IN C 2023 - 2024 - Sem2
BERC 1313 Lab 2 SELECTION CONTROL TECHNIQUE IN C 2023 - 2024 - Sem2
PROGRAMMING FUNDAMENTAL
2.
3.
1BERT S1/1 1BERT S1/2 1BERT S2/1 1BERT S2/2 1BERT S3/1
PROGRAMME/
SECTION
1BERE (Intake Sem2)
DATE 3.4.2024
2. MA TIEN CHOON
NAME OF
INSTRUCTOR(S)
3. TS. IMRAN BIN HINDUSTAN
Students' Names 2 /6
3 /6
Total Student 1 /6
Total Student 2 /6
Total Student 3 /6
0.0
1
1.0 PRE-LAB SURVEY
Tick the relevant response that describe you for each statement below:
Strongly Strongly
Agree Neutral Disagree
Agree Disagree
I am familiar with C Integrated
Development Environment.
I able to produce flowchart and write
program based on problem given.
I able to apply basic selection using if-
else
I able to apply basic selection using two
way if-else if-else
I able to apply basic selection using
switch-case
I able to write program that receive
inputs from user, do decision making
base on few choise, and display output
to user.
2.0 OBJECTIVES
1. To familiarize with C programming control structure.
2. To create a flowchart based on software requirement.
3. To perform programming by creating a flowchart.
4. To work effectively in a given task in the group.
2.0 EQUIPMENT
1. Personal Computer / desktop
2. Software: CodeBlocks
1
3.0 SYNOPSIS & THEORY
3.1 Decision Making
Executable statements either perform actions (calculations, input/output of data)
or make decisions. We might make a decision in a program, for example, to determine
the student’s grade on an exam whether passed or failed. So that, it is necessary for the
program to make a decision based on the truth or falsity of a statement of fact called a
condition. If the condition is true (condition is met), the statement in the body of if
statement is executed. If the condition is false (condition isn’t met), the body of the
statement isn’t executed.
Condition in selection statements is formed by using tha equality operators and
relational operators as in Table 2.1. The relational operators all have the same level of
precedence and they associate left to right. The equality operators have a lower level of
precedence than the relational operators and also associate from left to right.
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
≥ >= x >= y x is greater than or equal to y
≤ <= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y
Conditions in terms of the relational and equality operators only allow each
decision to be precisely tested by one condition. However, to test multiple conditions in
the process of making decision, we had to perform these tests in separate statements or
in nested if or if...else statements. C provides logical operators that may be used to
form more complex conditions by combining simple conditions.
2
Logical C logical Example of C condition Meaning of C condition
operators operator
if (gender == 1 && age >= 65) true if and only if both of the
AND
&& simple conditions are true
The && operator has higher precedence than ||. Both operrators associate from
left to right. However, both logical operators have lower precedence than equality and
relational operators. Except for NOT logical operator, it has a higher perecedence
among all other logical AND and OR operators as well as equality and relational
operators.
b) if...else selection
The if...else statement is called a double-selection statement because it
selects between two different actions. It will perform an action if a condition is
true and performs a different ation if the condition is false.
c) switch selection
The switch statement is called a multiple-selection statement because it
selects among many different actions. It will perform one of many different
actions, depending on the value of an expression.
3
Selection statements are used to choose among alternative course of action. The
if selection statement performs an indicated action only when the condition is true,
otherwise the action is skipped. As an example, lets say the passing mark on an exam is
60. The pseudocode statement
true
mark >= 60 print “Passed”
4
Figure 2.1: Flowcharting the single-selection if statement.
It will print Passed if teh student’s mark is greater than or equal to 60 and failed if the
student’s mark is less than 60. In either case, after printing occurs, the next pseudocode
statement in sequence is performed. Note that, the body of the else is also indented. The
if...else statement may be written in C as
The flowchart in Figure 2.2 illustrates the flow of control in the if...else
statement.
true
mark >= 60 print “Passed”
false
print “Failed”
5
Figure 2.2: Flowcharting the double-selection if…else statement.
6
Figure 2.3: Choosing drink with switch.
In the program, the user enters a letter to choose a drink. The getchar function reads
one character from the keyboard and stores that character in the chracter variable choice.
Keyword switch is followed by the variable name choice in parentheses. This is called
the controlling expression. The value of this expression is compared with each of the case
labels. Assume the user has entered the letter C as a choice. C is automatically compared to
each case in switch. If a match occurs (case ‘C’:) , the statements for that case are
executed. In the case of letter C, two printf statements are printed, and the switch
statement is exited immediately with the break statement.
The break statement causes the program control to continue with the first statement
after the switch statement. The break statement is used because the case in a switch
statement would otherwie run together. If a break is not used anywhere in a switch
statement, then each time a match occurs in the statement, the statements for all the remaining
cases will be executed. If no match occurs, the default case is executed, and an error
message is printed. Each case can have one or more actions. The switch statement is
different from all other control statements in that braces are not required around multiple
actions in the case of a switch. The general switch multiple-selection statement is
flowcharterd in Figure 2.4.
case a true
case a action(s) break
false
true
case b case b action(s) break
false
false
default action(s)
7
Figure 2.4: Flowcharting the multiple-selection switch statement.
4.0 PROCEDURE
Section A:
Using the if-else selection control structure, write a complete executable C program that
takes two non-negative numbers (exclude zero) as input from the user. The program is able to
determine which is the larger of the two numbers. The program also able to determine if both
numbers are equal.
The program should also tell which of the entered numbers is even or odd. For odd number/s,
find its square root and for even number/s calculate its value with power of 2 (i.e: 10 2 = 100).
Use the math function to find the square root and power of 2.
8
Section B:
Write 2 complete executable C program to calculate and display the tax payment based on
given flowchart below. Display your tax in 2 decimal places.
First program using if statement.
Second program using if-else statement.
9
Section C:
Using the case statement, write a program that displays the following menu:
Geometry Calculator If
1. Calculate the Area of a Circle.
2. Calculate the Area of a Rectangular.
3. Calculate the Area of a Triangle
4. Quit.
the user enters 1, the program should ask for the radius of the circle and then display its area.
Use the following formula:
area = pi * r2
Use 3.14159 for pi and radius of the circle for r.
If the user enters 2, the program should ask for the length and width of the rectangle and then
display the rectangle’s area. Use the following formula:
area = length * width
If the user enters 3, the program should ask for the length of the triangle’s base and its height,
and then display its area. Use the following formula:
area = base * height * 0.5
Input Validation: Display an error message if the user enters a number outside the range of 1
through 4 when selecting an item from the menu. Do not accept negative values for the
circle’s radius, the rectangle’s length or width, or the triangle’s base or height.
10
5.0 RESULTS
SECTION A
a. Write/Paste your flowchart inside a this block:
11
c. All possible inputs & Prediction based on your logic: [you can edit table as desired]
12
Table 1
13
SECTION B
a. Write/Paste your code inside a this block (if statement):
b. Write/Paste your output program based on code in Section B (a) in this block for all
possible cases:
Input Marriage
Age Output
Test Status
1 25 no Your Age is: 25
Your Salary (RM) is: 1500
Are you married (yes(y) or no(n)): n
14
Your tax is RM 150.00
#include <stdio.h>
int main()
{
int age;
float salary, tax;
char married;
15
if(married == 'y' || married == 'Y')
{
printf("\nYour tax is %.2f\n", salary*0.05);
}
else if(married == 'n' || married == 'N')
{
printf("\nYour tax is %.2f\n", salary*0.1);
}
else
{
printf("\nError input, Please enter yes or no.\n");
}
}
return 0;
}
d. Write/Paste your output program based on code in Section B (c) in this block for all
possible cases:
Input Marriage
Age Output
Test Status
1 45 Y
2 21 no
3 50 no
4 34 single
16
SECTION C
a. Write/Paste your code inside a this block:
#include <stdio.h>
int main()
{
int choice;
float r, length, width, base, height;
char Quit;
const float pi = 3.14159;
printf("Geometry Calculator\n\n");
printf("\tCalculate the Area of a Circle.\n");
printf("\tCalculate the Area of a Rectangular.\n");
printf("\tCalculate the Area of a Triangle.\n");
printf("\tQuit.\n");
switch (choice)
{
case 1:
printf("Please enter the radius of the circle :");
scanf("%f",&r);
if(r < 0)
{
printf("Please enter the positive value!!\n");
}
else
{
printf("The area of circle is : %.2f\n",pi*(r*r));
}
break;
case 2:
printf("Please enter the length of the rectangle:");
scanf("%f",&length);
printf("Please enter the width of the rectangle :");
scanf("%f",&width);
if(length < 0 || width < 0)
{
printf("Please enter the positive value!!\n");
}
else
{
printf("The area of rectangle is : %.2f\n",length*width);
}
break;
case 3:
printf("Please enter the length of the triangle's base :");
scanf("%f", &base);
printf("Please enter the height of the triangle :");
scanf("%f", &height);
if(base <0 || height<0)
{
17
printf("Please enter the positive value!!");
}
else
{
printf("The area of triangle is : %.2f\n",base*height*0.5) ;
}
break;
case 4:
printf("Existing program. \n");
Quit;
break;
default:
printf("Invalid choice. Please enter a number between1 and
4. \n");
}
return 0;
}
18
3
19
Note for lab invigilator: Please fill the following table if student being asked additional
question(s) or tak(s) during the laboratory session.
20