VNSGU BCA Sem 2: Programming Skills (204) Practical Solutions - April 2024 Set D¶
Paper Details
- Subject: Programming Skills Using C
- Subject Code: 204
- Set: D
- Semester: 2
- Month/Year: April 2024
- Max Marks: 25
- Time Recommendation: 2 Hours
- Paper: View Paper | Download PDF
Questions & Solutions¶
All questions are compulsory¶
Q1: Print N Numbers using UDF¶
Max Marks: 20
Write a python to print N number using UDF (User Defined Function).
Hint
Create a function that accepts N as parameter and prints numbers from 1 to N. Call this function from main part of program.
Solution Approach¶
flowchart TD
start[Start] --> input[Input N from user]
input --> call[Call print_numbers]
call --> loop[Loop from 1 to N]
loop --> print[Print current number]
print --> next{More numbers?}
next -->|Yes| loop
next -->|No| endfunc[Return to main]
endfunc --> end[End]
Solution - Python
def print_numbers(n):
"""
User Defined Function to print numbers from 1 to n.
Parameters:
n (int): The upper limit number to print
"""
print(f"\nNumbers from 1 to {n}:")
print("-" * 30)
for i in range(1, n + 1):
print(i, end=" ")
# Print newline every 10 numbers for readability
if i % 10 == 0:
print()
print("\n" + "-" * 30)
def print_numbers_with_format(n, format_type="line"):
"""
Alternative UDF with formatting options.
Parameters:
n (int): The upper limit number to print
format_type (str): 'line' for horizontal, 'column' for vertical
"""
print(f"\nPrinting numbers 1 to {n} in {format_type} format:")
print("=" * 40)
if format_type == "line":
for i in range(1, n + 1):
print(i, end=" ")
print()
elif format_type == "column":
for i in range(1, n + 1):
print(f"Number: {i}")
else:
print("Invalid format type. Use 'line' or 'column'.")
print("=" * 40)
# Main program
if __name__ == "__main__":
try:
# Get input from user
n = int(input("Enter the value of N: "))
if n <= 0:
print("Please enter a positive number greater than 0.")
else:
# Call the UDF
print_numbers(n)
# Demonstrate alternative formatting
print("\nAlternative formatting:")
print_numbers_with_format(n, "column")
except ValueError:
print("Invalid input! Please enter a valid integer.")
except Exception as e:
print(f"An error occurred: {e}")
Sample Output:
Enter the value of N: 15
Numbers from 1 to 15:
------------------------------
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15
------------------------------
Alternative formatting:
Printing numbers 1 to 15 in column format:
========================================
Number: 1
Number: 2
Number: 3
...
Number: 15
========================================
Alternative: Using Lambda Function
Alternative: Recursive Approach
Alternative: Using List Comprehension
Q2: Viva Preparation¶
Max Marks: 5
Potential Viva Questions
- Q: What is a User Defined Function (UDF)?
- A: A function created by the programmer to perform specific tasks, defined using
defkeyword. - Q: What is the purpose of the
defkeyword? - A: It is used to define/declare a function in Python.
- Q: What is the difference between argument and parameter?
- A: Parameter is in function definition, argument is the actual value passed when calling.
- Q: Can a function call itself?
- A: Yes, this is called recursion.
- Q: What is
if __name__ == "__main__":used for? - A: It ensures the code inside only runs when the script is executed directly, not when imported.
Common Pitfalls
- Range Upper Bound:
range(1, n+1)includes n,range(1, n)excludes n. - Input Validation: Always check if input is positive before calling function.
- Function Definition: Must define function before calling it.
Quick Navigation¶
Related Solutions¶
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Current Page |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026