EKT 150 - Introduction To Computer Programming Lab 2
EKT 150 - Introduction To Computer Programming Lab 2
LAB 2
Aim:
1. Learning the Open Source IDE Platform: CodeBlock Environment
2. Writing simple C Program based on Pseudo Code and Flow chart
3. To apply the “printf” and “scanf” functions to write simple input and output statements.
4. To use arithmetic operators and understand the arithmetic operators precedence
Figure 2
EKT 150 – Introduction to Computer Programming
LAB 2
3. Type “Hello World” program source code in the code block editor.
INTRODUCTION TO C
1. In C language, “printf” command is used to display any message or output to the screen.
The format of printf is:
printf(“The text to be displayed”);
2. The text that you want to display must be within the double quote “the text”.
3. You can send parameter to the printf function. The printf function enables us to display
the dynamic value after we have done the data processing, for example after calculating
mathematical problem and we want to display the answer to the screen. The command
we use is as below. For example, to calculate sum of two numbers using the equation,
sum=num1 + num2
printf(“The sum is %f”, sum);
• sum is the variable that contains the answer value, and it is passed to the printf
function.
• the symbol of % must be used to tell the printf function where to print the answer
value.
• The format “f” is used if we define the variable area as “float” data type. Other
format types are:
%d – for integer data type
%c – for character data type
%s – for string data type
%f – for floating points data type
and many more.
4. In the C language, scanf is used to accept the user input from the keyboard. The
command of scanf is as below:
scanf (“%f”,&base);
5. %f is the format of data type that will be entered. For example, if the variable “base” is
defined as float the format “f” is used.
LAB EXERCISES
#include<stdio.h>
int main(void)
{
printf("Introduction to Computer Programming\n");
printf("Total credit: \t 3 \n");
printf("Consist of 5 topics which are \nIntroduction \nSelection\n");
printf("Function \nRepetition \nArray \nPointers\n");
return 0;
}
Figure 1
EKT 150 – Introduction to Computer Programming
LAB 2
2. The following question are based on C code in Figure 2. Answer all questions.
#include<stdio.h>
int main()
{
//declaration of variables
int sum=10;
char letter='Z';
float set1=10;
double num1=10;
double num2=10e+10;
Figure 2
ii. Using the same C code in Figure 2, set the decimal place for float variable,
set1 to 3 decimal points, and double variable, num1 to 9 decimal points.
EKT 150 – Introduction to Computer Programming
LAB 2
3. C code in Figure 3 determines the average of 2 numbers input by users and display the
result at the end.
#include<stdio.h>
int main()
{
float number1,number2;
float average=0;
printf("Enter 2 numbers\n");
scanf("%f %f",&number1,&number2);
average=(number1+number2)/2;
return 0;
Figure 3
(ii) Rewrite the program in Figure 3 to accept input of 3 numbers and display the average
of those 3 numbers.
EKT 150 – Introduction to Computer Programming
LAB 2
1. Write a C program to read input of 2 floating numbers. The program must perform
addition, subtraction, multiplication and division against entered numbers. Display
each result of each mathematical operation. Your program output should be as
follows:
Enter 2 numbers:
45 23
2. Design an algorithm using flowchart for a program that measure area of triangle.
Then, write a complete C program to implement the algorithm. The program must
read the value of base and height of the triangle entered by users, measure the area of
the triangle and display the result at the end. The formula for triangle area given as
follows:
1
area = × base × height
2
EKT 150 – Introduction to Computer Programming
LAB 2
(i) Design the algorithm, i.e write the pseudo code and draw the flowchart of the
solution.
(ii) Implement the algorithm designed in (i) in your program. Test and verify the
completed program and write down your completed program.