Programming I
Programming I
Instructions:
Question 1 (6 Marks)
(a) Explain the purpose of the #include preprocessor directive in C. Give two common examples of
header files used with this directive and briefly describe what they provide.
(b) What is the difference between a variable declaration and a variable definition in C? Provide
a simple code example to illustrate both.
(c) Briefly explain the function of the printf() function in C. Give an example of how to print an
integer variable named count along with a descriptive label.
Question 2 (6 Marks)
(a) Write a short C program snippet that declares an integer variable age and uses an if-else
statement to check if the age is greater than or equal to 18. If it is, print "Adult"; otherwise, print
"Minor".
(b) Explain the difference between the while loop and the do-while loop in C. In what scenario
would you prefer to use a do-while loop over a while loop?
(c) What is the purpose of the return statement in a C function? What value does the main()
function typically return to indicate successful execution?
Question 3 (6 Marks)
(a) Define an array of 5 integers named numbers and initialize it with the values {10, 20, 30, 40,
50}. Write a C code snippet to print each element of the array using a for loop.
(c) Briefly explain the difference between passing arguments to a function "by value" and "by
reference".
Question 4 (6 Marks)
Write a C program that prompts the user to enter two integer numbers. The program should
then calculate and print the sum, difference, product, and quotient (integer division) of these
two numbers.
Question 5 (6 Marks)
Write a C function called calculate_factorial that takes an integer as input and returns its factorial.
Include error handling to ensure that the input is a non-negative integer. In your main() function,
prompt the user to enter a non-negative integer, call the calculate_factorial function, and print the
result.
Question 6 (6 Marks)
Write a C program that reads characters from the user until the user enters the character 'q'.
The program should count and print the total number of characters entered (excluding 'q').
Question 7 (6 Marks)
(a) Explain the concept of a string in C. How are strings typically represented and terminated?
(b) Write a C program that prompts the user to enter a string and then prints the length of the
string using the strlen() function from the string.h library.
End of Examination