Documente Academic
Documente Profesional
Documente Cultură
EQUIPMENTS:
No Component Quantity
1 Computer 1 unit
2 Codeblocks/Microsoft Visual C 1 unit
SAFETY PRECAUTION:
1. Do not plug in external devices (e.g USB thumb drive) without scanning them for
computer viruses
2. Always back up all your important data files.
PRACTICAL WORK:
PRACTICAL WORK:
Lab 5.1
#include <stdio.h>
int main()
{
int times;
int x;
printf("Sila masukkan berapa kali anda mahu ulang ayat ini : ");
scanf("%d",×);
}
return 0;
}
1
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
OUTPUT :
Lab 5.2
By using for statement, create a program that will display I WILL SCORE A IN
FUNDAMENTAL PROGRAMMING 7 times.
Sample output:
ANSWER :
Program
2
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
Lab 5.3
Create a program that will display numbers from 1 to 50 using while statement.
Sample output:
Answer:
Program
Lab 5.4
Write a program to produce the output that displays an odd numbers only. Use do-while
statement.
if ( number % 2 ! = 0 )
Sample output:
3
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
Answer:
Program
Lab 5.5
Based on lab 4.3, modify the program to produce the output that displays an even numbers
only. Use for statement.
Sample output:
Answer:
Program
4
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
Lab 5.6
Write a complete program that will display an output as below. Use FOR statements.
ANSWER :
Program
5
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
QUESTIONS :
1. What is one difference between while statement and do-while statement. (2 marks)
6
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
5. What is the final value of x when the code for (int x=0; x<10; x++) is run?
a) 10
b) 9
c) 0
d) 1
7.
#include<stdio.h>
int main()
{
int i=0;
for(i=1; i<=4; i++)
printf(%d,i);
getchar();
return 0;
}
7
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
int z =10;
while (z >2)
{
z--;
printf ( %d \t , z*2 );
}
Output :
int loopcounter=1;
c)
while ( loopcounter <= 10 )
{
printf ( "%d \t %d \n",loopcounter , loopcounter*loopcounter ) ;
loopcounter++;
}
Output :
8
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
9
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5
Succesful to make correct Succesful to make correct declaration Having difficulties to run the
declaration syntax. syntax in extended time. program and need assistance.
int times;
Lab 5.6
int x;
Declaration
float height;
float weight
float bmi;
Lab 5.6 Succesful to make correct printf Succesful to make correct printf and scanf Having difficulties to run the
Condition for and scanf for condition syntax. for condition syntax in extended time. program and need assistance.
looping scanf (%d, × );
Lab 5.6 Succesful to make correct FOR Succesful to make correct FOR Having difficulties to run the
Condition for statements. statements in extended time. program and need assistance.
looping (for(x=1; x<=times; x++)) (for(x=1; x<=times; x++))
Lab5.6 Succesful to used a correct Succesful to used a correct printf(), Having difficulties to used a
Compile and get printf(), scanf() and formula. scanf() and formula after being assisted. correct printf(), scanf() and
output formula, and need assistance.
Lab5.6 Succesful to used a correct Succesful to used a correct formular after Having difficulties to used a
Compile and get process being assisted. correct process and need
output bmi = weight / (height*height) assistance.
Lab 5.6 Successful program with neat Successful program after being assisted Having difficulties to run the
Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
output output in given time. to get output in extended time.
Total / 80
10