0% found this document useful (0 votes)
14 views2 pages

Lab5 W2024

The document provides instructions for 5 exercises on I/O and selection statements in C. The exercises involve reading input, checking for even/odd numbers, determining leap years, finding largest and smallest of 3 numbers, and calculating the check digit for an ISBN number. Hints are provided for using scanf to read single digits from a long input number.

Uploaded by

pedrofranca2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Lab5 W2024

The document provides instructions for 5 exercises on I/O and selection statements in C. The exercises involve reading input, checking for even/odd numbers, determining leap years, finding largest and smallest of 3 numbers, and calculating the check digit for an ISBN number. Hints are provided for using scanf to read single digits from a long input number.

Uploaded by

pedrofranca2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

(COMP-1400) Lab Exercises #5

Objective:
The main objective of this lab activity is to help students understand I/O and selection statements
in C.

Part A: I/O and Selection Statements

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.

> Please enter two numbers separated by a space: 10 7


> Remainder of 10 divided by 7 is 3.
> Please enter two more separated by a comma: 5, 3
> Remainder of 5 divided by 3 is 2.
> And two final numbers separated by a dash: 11-2
> Remainder of 11 divided by 2 is 1.

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).

> Please enter a number: 13


> 13 is an odd number.

Exercise 3: Write a C program to determine if a given year is a leap year.


Note: Leap year has 366 days instead of 365 days. Every 4 years we have a leap year. A leap
year is a non-century year which is evenly divisible by 4. A century year is the year which
ends with 00 (e.g., 1900, 2000, etc. ). Century year also can be a leap year if it is evenly
divisible by 400.

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.

> Please enter 3 numbers: 54, 23, 89 //comma separated


> The largest is 89.
> The smallest is 23.

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.)

For example, take the ISBN 978-0-306-40615-? :


a) First calculate sum of products:
9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3 = 93
b) Remainder upon dividing by 10 is 3.
c) Remainder is subtracted from 10: (10 - 3)
d) Check digit is 7.
For more information, watch the following Youtube video about finding the missing check digit
of an ISBN number: https://www.youtube.com/watch?v=WstjjL5CPqk

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

You might also like