COMSATS University Islamabad
Sahiwal Campus
(Department of Computer Science)
Course Title: Programming Fundamentals Course Code: CSC103 Credit Hours: 3+1
Course Instructor: Anam Khan Programme Name: BSCS
Semester: 1st Batch: SP25 Section: A+B Date: 14-03-25
Time Allowed: Maximum Marks: 10
Student’s Name: Reg. No. CUI/ /SWL
CLO4
CLOs Question Max. Obtained Question Max.
Obtained Marks
Addressed Nos. Marks. Marks Nos. Marks.
1 10
Important Instructions / Guidelines:
Read the question paper carefully and answer the questions according to their statements.
Mobile phones are not allowed. Calculators must not have any data/equations etc. in their memory.
Lab Assignment # 3
Objective:
The purpose of this assignment is to help students understand the application of conditional
statements in Java. The assignment will test the knowledge of Function, Arrays, Recursion,
Exception handling.
Instructions:
1. Write clean and well-commented code.
2. Use proper indentation and meaningful variable names.
3. Include input validation wherever necessary.
4. Submit your code with proper screenshots of the output.
CLO-4 Implement a program using programing constructs. Marks [1*7+3=10 ]
1. Write a method that computes the sum of the digits in an integer. Use the following
method header: public static int sumDigits(long n). For example, sumDigits(234) returns 9 (2 + 3
+ 4).
2. 10 students were asked to rate the quality of food in the student cafeteria, on a scale of 1 to
10 (1 means awful and 10 means excellent). Place the forty responses in an integer array and
summarize the results of the poll.
3. Find the largest and the smallest element in array. Then place largest element on 0th index
and smallest element on the last index 9 th. (Note: this should call three methods previously used
Input(int Array[]) , Largest(int Array[]) and Smallest (int Array[])
4. Write a program which performs the following tasks: • Initialize an integer array of 10
elements in main( ) • Pass the entire array to a function modify( ) • In modify( ) multiply each
element of array by 3 • return the control to main( ) and print the new array elements in main( )
Page 1 of 2
5. Write a Java program to accept a 3x4 integer array from user. You are required to: 1. Find
the row having maximum sum 2. Find the column having maximum sum.
6. This activity shows that how multiple exceptions can be handled by using a single try and
multiple catch blocks. In this activity the following exceptions are handled: •
InputMismatchException • ArrayIndexOutOfBoundsException.
7. Write a Java program to accept an array of 10 integer values from user. If the user enters a
wrong input then ask him to enter the input again. Once all 10 integer values are entered then
accept an index and display the value at that index. If the user has entered a wrong index then
your program should catch this exception.
8. Validate 4-Digit PIN Using Recursion
� Scenario (Real-Life Context)
In a banking system, customer security is of utmost importance. To enhance PIN validation
without relying solely on encryption or brute-force restrictions, a rule has been introduced:
"A customer's 4-digit PIN is considered valid only if the sum of its individual digits is less
than 20."
To implement this rule as part of the bank's validation software, you're tasked with writing a Java
program that automates this check.
� Requirements
1. The program must accept a 4-digit PIN number from the user via console input.
2. It must use recursion to calculate the sum of the digits of the entered number.
3. The program must validate that the entered number is exactly 4 digits long.
4. Based on the digit sum:
o If the sum is less than 20, the program should output:
�"PIN is VALID."
o If the sum is 20 or more, it should output:
�"PIN is INVALID (digit sum should be less than 20)."
5. If the user enters a number that is not 4 digits, the program should inform them of the
error with an appropriate message.
� Constraints
The program must use a recursive method to compute the sum of digits.No loops (for,
while) are to be used for summing digits.
Submission Guidelines:
1. Submit your code in a zip folder containing all Java files.
2. Include a PDF report with screenshots of the program execution.
3. The assignment must be submitted before the deadline.
4. Deadline 16-May-2025
The End
Page 2 of 2