Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LAB ACTIVITY 3

COMPUTER PROGRAMMING
BIC10204

Topics : Input and Output Operation and Formatting


• Understand input/output operation concept and output formatting
in C programming.

Learning Outcome : At the end of this lab session, students are able to know and understand the
concept of input/output operation in C programming

Software : DEV C++

Duration : 2 hours

Explanation :

Input and output operation in a C program involves the use of printf() and scanf()function.
The printf() function is used to prompt any message in order to trigger user respond for entering
a certain data. Whenever a program needs to read the data entered by user, scanf()function is
used. It works by reading the input data and store it in computer memory. Besides these two
functions, we can also use other I/O functions such as gets() and puts(), getchar() and
putchar() and lastly getch() and putch(). Below is example of a C program that performs
input and output operation using printf() and scanf()function.

#include <stdio.h>

int main()
{ Ampersand symbol (&) for location reference
char name[20];
int mark;

printf(“Please enter your name : ”);


scanf(“%s”,&name);
printf(“Please enter your mark : “);
scanf(“%d”,&mark);
printf(“\nYour name is %s and your mark is %d”, name, mark);

} Placeholder (%d)
Print list – can be
variable/constant/expression/function
Placeholder symbols to remember are stated below:

%d Integer
%f Decimal
%c Character
%s String

1.

1
a.
#include <stdio.h>

void main()
{
int num;
char character;

printf(“\nEnter one character :”);


num = getc(stdin);
character = num;
printf(“\nThe character that has been entered is “);
putc(character, stdout);
printf(“\nThe value of %c in integer is %d”,
character, num);
}

b.

#include <stdio.h>

int main()
{
char character;

printf(“\nEnter one character :”);


character = getchar();
printf(“\nThe character that has been entered is “);
putchar(character);
}

c.

#include <stdio.h>

int main()
{
char name[20];

printf(“\nEnter your name :”);


gets(name);
printf(“\nYour name is “);
puts(name);
}

2
2. Write a complete program following the algorithm given below.

2.1 Print a message to ask user to enter their name.


2.2 Read name.
2.3 Print a message to ask user to enter their height and weight in kg and m.
2.4 Read weight and height.
2.5 Print name.
2.6 Print weight and height.

3. Write a complete program following the algorithm given below.

3.1 Declare identifier of integer data type named num1.


3.2 Declare identifier of float data type named num2.
3.3 Declare a constant called MAX with a value of 50.88.
3.4 Print a message to ask user to enter value for num1.
3.5 Read num1.
3.6 Print a message to ask user to enter value for num2.
3.7 Read num2.
3.8 Print a message to ask user to enter value for MAX.
3.9 Read MAX.
3.10 Print num1, num2 and MAX.

4. Develop a simple subject registration system. The program requires user to enter student’s full
name, I/C number, matric number, subject code and subject name. Then the program will print out
all the data that has been entered on the screen.

You might also like