SELF-PACED LEARNING
MODULE
College
INFORMATION SHEET PR-5.1.1
“Input and Output Statements”
Input Statement – a statement used to input a single character or a sequence of characters from the
keyboard
1. getch – a function used to input a single character from the keyboard without echoing the
character on the monitor.
Syntax: getch()
Ex: ch=getch();
2. getch – a function used to input a single character from the keyboard, the character pressed
echoed on the monitor, like READLN in PASCAL
Syntax: getche();
Example: ch = getche();
3. getchar – a function used to input a single character from the keyboard, the character
pressed echoed on the monitor, terminated by pressing ENTER key.
Syntax: getchar();
Example: ch = getchar();
4. gets – a function used to input sequence of character from the keyboard, space are
accepted, terminated by pressing enter key
Syntax: gets();
Example: gets(ch);
5. scanf – a function used to input single characters from the keyboard, it needs the control
string codes in able to recognized. Space are not accepted upon inputting. Terminated by
pressing spacebar
Syntax: scanf(“control string codes”, identifier).
Example: scanf(“%d”, &num);
Output Statement – a statement used to display the argument list or string on the monitor
1. printf – a function used to display the argument list on the monitor. It sometimes needs the
control string codes to help display the remaining argument on the screen.
Syntax: printf(“control string codes”, argument list);
Example: printf(“Hello %s, you are %d years old”, name, age);
2. putchar – a function used to display the argument list or string on the monitor. It is like
overwriting a character.
Syntax: putchar();
Example: putchar(tolower(ch));
3. puts – a function used to display the argument list or string on the monitor. It does not need
the help of the control string codes
Syntax: puts()
Example: puts(“hello”);
Format Strings and Escape Sequences
All format specifiers start with a percent sign (%) and are followed by a single letter indicating
the type of data and how data are to be formatted.
List of commonly used format specifiers:
% c – used for single char in C
scanf(“%c”, &ch);
printf(“%c”, ch);
%d – decimal number (whole number)
scanf(“%d”, &num);
printf(“%d”, num);
%e – scientific notation / exponential form
scanf(“%e”, &result);
printf(“%e”, result);
%f – number with floating or decimal point
scanf(“%f”, &pesos);
printf(“%f”, pesos);
%o – octal number
scanf(“%o”, &value);
printf(“%o”, value);
%s – string of characters
scanf(“%s”, &str);
printf(“%s”, str);
%u – unsigned number
scanf(“%u”, &nos);
printf(“%u”, nos);
%x – hexadecimal number
scanf(“%x”, &value);
printf(“%x”, value);
%X – capital number for hexadecimal number
scanf(“%X”, &nos);
printf(“%X”, nos);
%% - print a percent sign
scanf(“%%”, &value);
printf(“%%”, value);
List of commonly used escape sequences
\\ - prints backslash
\’ – prints single quotes
\” – prints double quotes
\? – prints question mark
\n – newline
gotoxy – is used to send the cursor to the specified location.
Syntax: gotoxy(x,y);
Example: gotoxy(5, 10);
Examples of Input and Output Program.
1. Make a program that will display the word “Hello”, “I’m fine”, and “Thank You” in a different
lines and column.
#include <stdio.h>
#define g gotoxy Output:
main() Hello
{ I’m Fine
clrscr(); Thank You
printf(“Hello”);g(5,2);
printf(“I\’m Fine”);g(10, 3);
printf(“Thank You”);
getch();
}
2. Create a program that accepts and displays a person’s name and age.
#include <stdio.h>
#define p printf Output:
main() Enter Name: Paul
{ Enter Age: 3
int age;char name[10]; Hello Paul, you are 3 years old
clrscr();
p(“\n Enter name:”);
scanf(“%s”, &name);
p(“\n Enter Age:”);
scanf(“%d”, &age);
p(“\n Hello %s, you are %d years old”, name, age);
getch();
}
3. Make a program that will compute and display the sum and product of two numbers.
#include <stdio.h>
Output:
First number: 5
Second number: 6
Sum of two numbers: 11
Product of two number: 30
#define p printf
#define s scanf
main()
{
intA,B,Sum, Prod;
clrscr();
Sum = 0; Prod = 0;
p(“\n First number:”); s(“%d”, &A);
p(“Second number:”); s(“%d”, &B);
Sum = A + B; Prod = A * B;
p(“\n Sum of two numbers: %d”, Sum);
p(“\n Product of two numbers: %d”, Prod);
getch();
}
Note: An assignment statement stores a value or a computational result in a variable. They are
commonly used to perform most arithmetic operations in a program. It can also be used in printf()
statement.
4. Equivalent C program of Example no.3 using assignment statement in printf() statement
#include <stdio.h>
#define p printf Output:
#define s scanf First number: 5
main() Second number: 6
{
intA,B,Sum, Prod; Sum of two numbers: 11
clrscr(); Product of two number: 30
Sum = 0; Prod = 0;
p(“\n First number:”); s(“%d”, &A);
p(“Second number:”); s(“%d”, &B);
p(“\n Sum of two numbers: %d”, Sum = A+ B);
p(“\n Product of two numbers: %d”, Prod = A*B);
getch();
}
Note: A format specifier%.2f can be used to limit the output being displayed into two decimal places
only.
Reference:
Workbook in C Programming Computer Programming 1 by Paulino H. Gatpandan, Azenith M. Rollan
page 54-59.
SELF CHECKPR-5-1-1
Give the meaning of the following format specifiers:
1. %% - _______________________
2. %s - _______________________
3. %f - _______________________
4. %X - _______________________
5. %c - _______________________
6. %x - _______________________
7. %d - _______________________
8. %u - _______________________
9. %e - _______________________
10. %o - _______________________
STUDENT NAME: __________________________________ SECTION: __________________
PERFORMANCE TASK PR-5.1.1
PERFORMANCE TASK TITLE: Input and Output Program
PERFORMANCE OBJECTIVE: The learners independently demonstrate core competencies about Input
and Output Program
MATERIALS:
Computer / Android phone
Applications / Software:
Computer: TurboC2
Android phone: Coding C / CppDroid – C/C++ IDE
Procedure:
A. Make a program that will compute and display the sum and average of three
numbers
B. Make a program that will convert an inputted number in inches (in.) and
display its equivalent measure in feet (ft)
C. Create a program that asks for a distance in kilometers and converts it into its
metric equivalent
D. Put the screenshot of the output and code here.
PRECAUTIONS:
Undertake final inspections to ensure the program conform to requirements
ASSESSMENT METHOD: PERFORMANCE TASK CRITERIA CHECKLIST
STUDENT NAME: __________________________________ SECTION: __________________
PERFORMANCE TASK CRITERIA CHECK LIST PR-5.1.1
SCORING
CRITERIA
1 2 3 4 5
1. Quality of Work – the ability to follow procedures with precision and the
art, skill and finality of work
2. Speed – efficiency of work
3. Proper use of statement – the ability to apply proper statement for a
given task
TEACHER’S REMARKS: QUIZ RECITATION PROJECT
GRADE:
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_________________________________
TEACHER
Date: ______________________