Lab5 W2024
Lab5 W2024
Objective:
The main objective of this lab activity is to help students understand I/O and selection statements
in C.
Exercise 1: In this exercise we read two inputs from the user in three different ways. The message
to the user and the format they input the text is expected to remain exactly as shown. You should
use at most 3 scanf statements for this task.
Exercise 2: Write a C program to get an integer number and check whether the given number is
even or odd. (Note each exercise is independent, so this should be coded in a new C file).
Sample 1:
> Please enter a year: 1900
> The year 1900 is not a leap year.
Sample 2:
> Please enter a year: 2000
> The year 2000 is a leap year.
Exercise 4: Write a C program that receives three integer values from the user and displays the
largest and the smallest of the three.
Page 1 of 2
Part B: Calculate the missing check digit in ISBN
What is an ISBN?
An International Standard Book Number (ISBN) is a unique number
assigned to each book. ISBN-13 has thirteen digits in 5 parts, in which
the last digit is the “check digit”. The check digit is base ten, and can be
0-9. To compute a missing check digit, each digit, from left to right, is
alternatively multiplied by 1 or 3. Then, the sum of these products should
be divided by 10 to find the remainder ranging from 0 to 9. Finally, the
check digit can be found by subtracting the remainder from 10, that leaves
a result from 1 to 10. (Note that if the remainder is 0, then we don’t
subtract from 10 and use 0 as the check digit.)
Exercise 5: Write a C program that gets the first twelve digits of ISBN-13, and displays the
corresponding check digit. However, you can only use the scanf(...) function once (it can’t be used
within a loop). The program must match the sample input/output messages given below. Note that
the input is given as a 12-digit number not separated by commas or spaces.
Sample:
Enter the first twelve digits of ISBN-13: 978030640615
The check digit is 7.
Hint: A 12-digit number is too large to fit inside an integer which can hold at most a small 10-
digit number. However, we can read single digits very easily with the scanf function. We can
provide the scanf function the %1d conversion specification to do this. The number that comes
between “%” and “d” in the conversion specifier indicates the maximum length of the integer that
scanf reads from the input. For example, if the input given is 12345, and we use the line
scanf(“%1d%3d”, &num1, &num2); we will first obtain num1 = 1 (first digit), and finally num2
= 234 (the next 3 digits). We still have the number 5 to read from the input.
Practice problem (not graded): Try to solve the above problem without using the %1d
conversion specifier while still only using one scanf.
EVALUATION: You need to show your GA/TA the complete code solution during the lab. The
marks you will receive for this lab are made of two parts: Lab work marks 8 and attendance marks
2. Total 10 marks.
Page 2 of 2