VNSGU BCA Sem 2: Programming Skills Using C (204) Practical Solutions - Set AΒΆ
Paper Details
- Subject: Programming Skills Using C
- Subject Code: 204
- Set: A
- Semester: 2
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & SolutionsΒΆ
All questions are compulsoryΒΆ
Q1: Factorial UDFΒΆ
Max Marks: 10
Write a C program to create user define function fact() that returns factorial of given number N to main function.
1. Factorial Function LogicΒΆ
Develop the fact() function to calculate the factorial using a loop.
Hint
Factorial of \(N\) is \(1 \times 2 \times \dots \times N\). Use a loop that starts from 1 and multiplies each number. Handle the case for \(0! = 1\) correctly.
flowchart TD
start[Start Function]
init[Initialize Result = 1]
loop[Loop from 1 to N]
calc[Result = Result * i]
ret[Return Result]
start --> init
init --> loop
loop --> calc
calc -- "Check Loop" --> loop
loop --> ret
View Solution & Output
// User-defined function to calculate factorial
int fact(int n) {
if (n == 0 || n == 1) {
return 1;
}
int result = 1;
for (int i = 2; i <= n; i++) {
result = result * i;
}
return result;
}
Step-by-Step Explanation:
1. Initialization: Initialize result to 1 and check if the input n is 0 or 1.
2. Logic Flow: Use a for loop from 2 to n, multiplying result by each integer to calculate the factorial.
3. Completion: Return the final result value to the main function for display.
2. Main Execution FlowΒΆ
Implement the main() function to accept user input and call the fact() function.
View Solution & Output
#include <stdio.h>
int main() {
int N;
printf("Enter a number: ");
scanf("%d", &N);
if (N < 0) {
printf("Error: Factorial not defined for negative numbers.\n");
} else {
int factorial = fact(N);
printf("Factorial of %d = %d\n", N, factorial);
}
return 0;
}
Step-by-Step Explanation:
1. Initialization: Declare an integer variable N and prompt the user for input.
2. Logic Flow: Read the number, validate that it is non-negative, and call the fact() UDF.
3. Completion: Print the calculated factorial or an error message if the number was negative.
Output:
Q2: Python Student CRUDΒΆ
Max Marks: 10
Write a Python program to create a dictionary for student data which contain Roll No, Name, Div, Marks and City. Perform Insert, Update and Delete operation on it.
1. Data Initialization & CRUD OperationsΒΆ
Create the student dictionary and navigate through the different operations.
Hint
Start with a dictionary containing the required fields. Use student['new_key'] = value for insert/update and del student['key'] for delete.
flowchart TD
dict[Student Dict]
ins[Insert New Key]
upd[Update Value]
del[Delete Key]
dict --> ins
ins --> upd
upd --> del
View Solution & Output
# Initialize student dictionary
student = {
"Roll No": 101,
"Name": "Rahul Sharma",
"Div": "A",
"Marks": 85,
"City": "Surat"
}
# Display original data
print("Original Student Data:", student)
# INSERT operation
student["Phone"] = "9876543210"
print("After Insert (Phone):", student)
# UPDATE operation
student["Marks"] = 92
print("After Update (Marks):", student)
# DELETE operation
del student["Div"]
print("After Delete (Div):", student)
Step-by-Step Explanation:
1. Initialization: Define a dictionary student with initial fields like Roll No, Name, and Marks.
2. Logic Flow: Perform an insert by adding a new key, update an existing value, and delete a key using the del keyword.
3. Completion: Print the dictionary state after each operation to confirm the CRUD actions.
Expected Output:
Original Student Data: {'Roll No': 101, 'Name': 'Rahul Sharma', 'Div': 'A', 'Marks': 85, 'City': 'Surat'}
After Insert (Phone): {'Roll No': 101, 'Name': 'Rahul Sharma', 'Div': 'A', 'Marks': 85, 'City': 'Surat', 'Phone': '9876543210'}
After Update (Marks): {'Roll No': 101, 'Name': 'Rahul Sharma', 'Div': 'A', 'Marks': 92, 'City': 'Surat', 'Phone': '9876543210'}
After Delete (Div): {'Roll No': 101, 'Name': 'Rahul Sharma', 'Marks': 92, 'City': 'Surat', 'Phone': '9876543210'}
Q3: Viva PreparationΒΆ
Max Marks: 5
Potential Viva Questions
- Q: How does a function return a value in C?
- A: Using the
returnstatement followed by the value or variable to be sent back to the calling function. - Q: What is a Python Dictionary?
- A: An unordered collection of data in key-value pairs, where keys must be unique and immutable.
- Q: Difference between
int fact()andvoid fact()? - A:
int fact()returns an integer value, whilevoid fact()does not return any value. - Q: What is Recursion? Can we solve factorial using it?
- A: Recursion is when a function calls itself. Yes, factorial is a classic example:
fact(n) = n * fact(n-1)with base casefact(0)=1. - Q: Are dictionary keys case-sensitive in Python?
- A: Yes, "Name" and "name" would be treated as two different keys.
- Q: What happens if you try to access a dictionary key that doesn't exist?
- A: Python raises a
KeyError. You can use the.get()method to avoid this and returnNoneinstead.
Quick NavigationΒΆ
Related SolutionsΒΆ
| Set | Link |
|---|---|
| Set A | Current Page |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2025