CLO 2P
POLITEKNIK SULTAN IDRIS SHAH
JABATAN KEJURUTERAAN ELEKTRIK
DEC20012 - PROGRAMMING FUNDAMENTALS
Title Selection Structure in C
Practical
3i
Work
Course DEP 2A / DEP 2B / DTK 2A / DTK 2B
Session June 2019
Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.
OBJECTIVES :
Upon completion of this practical, students should be able:
1. To construct programs that uses simple if, if else, nested if else and switch
statements
EQUIPMENTS :
1. Personal computer / Laptop
2. Dev-C++ software
Lab 3.1 Conditional Structure (if)
Procedures:
3.1.1 Write the following programming below and try to understand it.
// This program demonstrates the use of if conditional structure
#include <stdio.h>
int main ()
{
int marks;
printf(" What would be your targeted marks for DEC20012 ?\n");
scanf("%d", &marks);
if(marks==100) // If this condition is true, statement is executed
{
printf("I would like to get %d marks for DEC20012 subject", marks);
}
system("pause");
return 0;
}
3.1.2 Write the above program and save it as Practical31. To compile, click on
Execute and choose Compile. Alternatively the program can be compiled
by using Ctrl + F9 hotkey.
3.1.3 Run the program and capture the codes, Compile progress and output.
To Run, simply click on Execute > Run. Alternatively hit the Ctrl + F10.
1
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Lab 3.2 Conditional Structure (if else)
Procedures:
3.1.4 Write the following programming below and save it as Practical32. In your
own word discuss your understanding of if else statement.
// This program demonstrates the use of if conditional structure
#include <stdio.h>
int main ()
{
int marks;
printf(" What would be your targeted marks for DEC20012 ?\n");
scanf("%d", &marks);
if(marks==100) // If this condition is true, statement is executed
{
printf("I would like to get %d marks for DEC20012 subject", marks);
}
else
{
printf("I should have set my sight higher for DEC20012..\n");
}
system("pause");
return 0;
}
Lab 3.3 Conditional Structure (if… else if… else)
Procedures:
3.1.5 Write the following programming below and save it as Practical33. Discuss
your understanding of if else if statement.
#include<stdio.h>
void main()
{
int marks;
printf(" Please enter your mark: ");
scanf("%d",&marks);
if(marks>=70){
printf("You Passed\n");
}
else if(marks>=50){
printf("Moderate\n");
}
else {
printf("You Failed\n");
}
system("pause");
}
2
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Lab 3.4 Now you try!
Procedures:
3.1.6 By taking Lab 3.1 until Lab3.3 into consideration, solve the following task.
3.1.7 Write your codes by using Dev-C++ software and check for any errors.
3.1.8 Rewrite your codes in the spaces provided.
Task A (if statement)
Write a program that prompt and get two numbers, compare the two numbers and
display “They are equal” if (num1==num2).
Answer:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
3
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Task B (if statement)
Alter the codes at Task A so that the program will display “num1 is less
than num2” if (num1<num2) and “num1 is greater than num2” if (num1>num2).
Answer:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
4
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Task C (if statement)
Design a program that will display the following output by using if statement
in your program. It can be considered chilly if the temperature drop more
than 65oF.
What is the temperature outside? 70
How pleasant!
What is the temperature outside? 20
It’s a bit chilly out!
Answer:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
5
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Task D (if else statement)
Write the given program and trace the output.
#include <stdio.h>
int main()
{
char answer;
printf(“Do you like donuts? (y/n) \n”);
scanf (“%c”,&answer);
if ((answer==’y’) || (answer==’n’))
{
printf(“Good job, you didn’t mess anything up”);
}
else
{
printf(“Keyboard much?”);
}
getchar();
return 0;
}
Answer:
Output
a) If answer = y
b) If answer = n
c) If answer = r
6
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Task E (if else statement)
Modify the program at Task C by using if else statement to get the same
output
Answer:
[80 Marks]
7
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Questions
1. What would be the output for the following C code? (1 marks)
#include<stdio.h>
int main()
{
int a=2;
if (a==2)
{
a=-a+2<1;
printf(“%d”,a);
}
else
{
break;
}
}
A. It will print nothing B.0
C. -2 D. Compile error.
2. Which control structure does the following flowchart represents in C? (1 marks)
A. if B. nested if else
C. if else D. switch
8
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
3. a) Fill in the blank (6 marks)
b) Trace the output : (2 marks)
#include <stdio.h>
____________
{
int num;
printf("Enter a number you want to check.\n");
scanf("____",_____);
____ ((num%2)==0)
{
printf("%d is even.", _____);
}
else
{
printf("%d is odd.",num);
}
__________;
}
Output : (if num=25) (1 mark)
Output : (if num=2) (1 mark)
9
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
4. a) Fill in the blank (5 marks)
b) Trace the output (2 marks)
c) Draw a flowchart (3 marks)
_______________
void main()
{
int result;
printf("Enter result: ");
________ ("%d",&result);
if (result >=45)
{
printf("Passed\n");
______("Congratulations\n")
}
_____
{
printf("Failed\n");
printf("Good luck in the
resits\n");
}
getchar();
___
Output : (if result=60) (1 mark)
Output : (if result=20) (1 mark)
10
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i
CLO 2P
Flowchart: (3 marks)
Prepared by: Checked by: Approved by:
MOHD ZEID BIN ABU BAKAR
PENSYARAH
JABATAN KEJURUTERAAN ELEKTRIK
POLITEKNIK SULTAN IDRIS SHAH
11
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3i