This part of the lab is worth 5 marks.
Every student in NUS has been assigned an undergraduate matriculation number of the form Ud1d2d3d4d5d6X in which U (denoting Undergraduate) is followed by a six-digit number d1..d6 and a single check-letter X. The check-letter is useful in checking the validity of the matriculation number especially in computerised systems which are prone to human error during the data entry process.
Note that you are NOT to use any form of arrays in your program.
Result from step 3 Check-Letter 1 M 2 B 3 N 4 A 5 R 6 E 7 U 8 H 9 W 10 J 11 X 12 L 13 Y
The following code continuously accepts the matric number and check-letter from the user and simply prints the matric number entered. The program terminates when the matric number is zero or less. You are encouraged to modify and extend the skeleton code in order to achieve your task.
#include <stdio.h> int main() { int matric; char ref; /* Input a six-digit matric number */ printf("Enter the six-digit matric number (0 or -ve to exit): "); scanf("%d", &matric); while (matric > 0) { /* Input the check-letter */ printf("Enter the capitalized check-letter: "); /* flush input stream to clear any existing character(s) and then scanf*/ fflush(stdin); scanf("%c", &ref); printf("The matric number entered is U%d%c\n", matric, ref); /* Input another six-digit matric number */ printf("Enter the six-digit matric number (0 or -ve to exit): "); scanf("%d", &matric); } /* Terminating program */ printf("\nProgram Terminated...\n"); return 0; }
The following is a sample session. User input is denoted in bold.
Enter the six-digit matric number (0 or -ve to exit): 012345 Enter the capitalized check-letter: X The matric number U12345X is valid. Enter the six-digit matric number (0 or -ve to exit): 012345 Enter the capitalized check-letter: A The matric number U12345A is INVALID! Enter the six-digit matric number (0 or -ve to exit): 0 Program Terminated...
You are encouraged to run your program using your own matric number, as well as the matric numbers of your neighbours or even your lab tutors (as long as their matric number begins with a U).