1. a) How can comments (remarks) be included within a C program?
Where
can comments be
placed? (5 Marks)
In C, comments are used to add explanatory notes within the code, making it
easier to understand for programmers. Comments are ignored by the compiler,
meaning they do not affect the execution of the program.
Comments in the program
In the C programming language, // and /* ... */ are two different ways to
represent comments, which are annotations in the code that are ignored by
the compiler.
The // syntax is used for single-line comments.
The /* ... */ syntax is used for multi-line comments.
Comments can be placed:
Before a code block to describe its purpose.
Beside a statement for quick explanations.
At the beginning of a program to provide general information like the
program's purpose, author, and date.
Inside functions to explain logic.
Between lines of code to describe complex sections.
b) Name and describe the four basic types of constants in C. (4 Marks)
Constants
Fixed values that do not change during the execution of a program. There are
different types of constants in C programming
Integer Constant 10, 20, 450 etc. Real or
Floating-point Constant 10.3, 20.2, 450.6 etc.
Character Constant ’a’, ’b’, ’x’ etc.
String Constant ”c”, ”c program”.
2. Can a keyword be used as an identifier? Justify your answer. Also, list at
least 5 C keywords and explain their purpose in C.(9marks)
No, a keyword cannot be used as an identifier in C.
Justification:
Keywords are reserved words that have special meanings in the C
language.
They are predefined and serve specific purposes in program structure
and syntax.
Using a keyword as an identifier (e.g., a variable or function name)
would cause a compilation error since the compiler expects the
keyword to perform its designated function.
Eg) int return = 10; // Error: 'return' is a keyword
This results in a syntax error because return is a reserved keyword used for
returning values from functions.
Five C Keywords and Their Purpose:
int
Used to declare integer-type variables.a
Eg) int age = 25; // Declares an integer variable 'age'
return
Used to return a value from a function.
Eg) int sum() {
return 10 + 20; // Returns the sum
if
Used for conditional statements.
Eg)if (age > 18) {
printf("Adult");
while
Used for loop execution as long as a condition is true.
Eg)int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
void
Specifies that a function does not return a value.
Eg)void greet() {
printf("Hello, World!");
3. a) Discuss the key features of C programming language. (6 Marks)
1. Simple and Efficient – Provides a structured approach with clear
syntax, making it easy to understand and efficient in execution.
2. Fast Execution – Programs written in C run quickly due to direct
interaction with system hardware.
3. Portability – Code written in C can be compiled and run on different
operating systems with minimal modifications.
4. Rich Library Support – Includes standard libraries (stdio.h, math.h,
etc.) for various functionalities.
5. Memory Management – Provides direct access to memory using
pointers and functions like malloc() and free().
6. Structured Programming – Supports functions, loops, and conditional
statements, allowing modular program design.
b) How do constants differ from variables in C? (3 Marks)
4. What are constants and keywords in C? Provide examples.
Constants – Fixed values that do not change during program execution.
Eg)#define MAX 100
const float PI = 3.1415;
Keywords – Reserved words with predefined meanings in C
Eg)int num = 10; // 'int' is a keyword
if (num > 5) { // 'if' is a keyword
return 1; // 'return' is a keyword
5. How do variables and data types in C contribute to the efficiency and clarity
of a program? Provide an example.
· Variables store data dynamically, making the program flexible.
· Data types ensure memory efficiency by allocating appropriate storage.
· Improves readability by clearly defining what kind of data is being handled.
Eg)
#include <stdio.h>
int main() {
int age = 20; // Integer variable
float height = 5.8; // Floating-point variable
printf("Age: %d, Height: %.1f", age, height);
return 0;
}
[Link] identifiers. Give rules for declaring identifiers.
What are Identifiers?
Identifiers are names given to variables, functions, and other user-defined
elements in a C program.
Rules for Declaring Identifiers:
1. Must start with a letter (A-Z or a-z) or an underscore (_).
2. Can contain letters, digits (0-9), and underscores.
3. Cannot be a C keyword (e.g., int, return).
4. Case-sensitive (Age and age are different).
5. Should be meaningful and descriptive.
7. Write a C program to calculate the area and perimeter of a rectangle. What
are the appropriate data types for the inputs (length and width) and the
calculated values (area and perimeter), and why?(9)
Appropriate Data Types:
float or double for length and width: These values may include
decimals (e.g., 5.5, 10.2).
float or double for area and perimeter: Since they are computed using
length × width or 2 × (length + width), floating-point precision is needed.
Why float?
Length and width can be decimal values.
Area and perimeter calculations may result in decimal numbers.
8. a) What is the most suitable variable type to represent the area of a circle in
square inches, and why? (3 Marks)
The most suitable data type is double.
The area of a circle is calculated as π × r², which involves floating-point
calculations.
double provides higher precision than float, reducing rounding errors.
Example declaration:
double area;
b) Write a C program that accepts the following inputs: a student's name
(string), age (integer), and GPA (float). Then,display the student's details in a
formatted output. What are the appropriate data types for each input, and why?
(6 Marks)
Appropriate Data Types:
char[] for name – A string of characters.
int for age – Whole numbers only.
float for GPA – May include decimal values
Why These Data Types?
char name[50]: Holds a sequence of characters (name).
int age: Stores whole numbers.
float gpa: Allows decimal values.
9. a) Why is it important to initialize variables before using them in a program?
(3 Marks)
· Avoids Garbage Values – Uninitialized variables may contain
unpredictable values, leading to incorrect results.
· Prevents Undefined Behavior – Using an uninitialized variable can cause
errors or crashes.
· Improves Code Reliability – Ensures predictable program execution.
b) Describe the fundamental data types in C. (6 Marks)
10. What will be the output of the following C code and why?
int a = 5, b = 2, c;
c = a / b;
printf("%d", c);
Output= 2
Explanation:
a and b are both declared as int, meaning integer division is performed.
5 / 2 in integer division discards the decimal part and results in 2, not
2.5.
Since c is also an int, it stores 2 as the final result.
11. Explain the difference between float and double data types in C.
12. What will be the value of x after execution of the following C code and why?
int x,y=10;
char z=’a’;
x=y+z;
107
Explanation:
char z = 'a' → The ASCII value of 'a' is 97.
y + z → 10 + 97 = 107.
Since x is an int, it stores 107.
13. Explain the different bit-wise operators available in C. Write a C program
that demonstrates the use of the following bit-wise operators: &, |, ^ on two
integer variables. (9marks)
output
a&b=1
a|b=7
a^b=6
14. Describe the concept of operators in C and explain the various types with
examples. (9 marks)
· Arithmetic Operators (For mathematical operations)
+, -, *, /, %
Example: int sum = 10 + 5;
· Relational (Comparison) Operators (For comparisons)
==, !=, >, <, >=, <=
Example: if (a > b)
· Logical Operators (For boolean logic)
&& (AND), || (OR), ! (NOT)
Example: if (x > 5 && y < 10)
· Bitwise Operators (Operate on bits)
&, |, ^, ~, <<, >>
Example: a & b
· Assignment Operators (Assign values)
=, +=, -=, *=, /=, %=
Example: a += 5;
· Increment and Decrement Operators
++ (Increment), -- (Decrement)
Example: x++
· Ternary Operator (?:)
Shorthand for if-else
Example: int min = (a < b) ? a : b;
· Sizeof Operator
Returns the size of a data type
Example: sizeof(int)
· Comma Operator
Used in loops and variable declarations
Example: int a = (b = 5, c = 10);
15. a) How can modulus and division operators be used to manipulate
numbers? (3 Marks)
Modulus (%) – Returns the remainder of a division.
Division (/) – Returns the quotient.
Eg)
int x = 10, y = 3;
printf("%d", x / y); // Output: 3 (quotient)
printf("%d", x % y); // Output: 1 (remainder)
Useful for:
Checking if a number is even (x % 2 == 0).
Extracting digits (x % 10 gives last digit of a number).
b) What is the result of the following C code? Explain your answer based on
operator precedence and the behavior of the increment operators. (6 Marks)
int x = 5,y=10,result; result = x++ + ++y;
Step-by-Step Execution:
1. x++ → Uses x = 5, then increments x to 6.
2. ++y → Increments y to 11, then uses 11.
3. result = 5 + 11 = 16.
Final Values:
x=6
y = 11
result = 16
16. Explain the increment and decrement operators in C programming with
examples. (3 marks)
17. What is the significance of operator precedence in a C program, and how
does it impact the order of execution in expressions? (3 marks)
Operator precedence determines the order of evaluation in expressions.
Impact:
Operators with higher precedence execute first.
Parentheses () can override precedence.
18. Differentiate between the equality operator and the assignment operator
with examples.(3 marks)
18. Write a C program that prompts the user to enter a number and then
computes and displays the factorial of that number.(9)
20. Explain the formatted and unformatted I/O functions of the C language.
(9)
Formatted I/O Functions:
Used for structured input/output with format specifiers.
printf(): Displays output in a formatted manner.
scanf(): Reads formatted input from the user.
Eg)
Unformatted I/O Functions:
Work with raw input/output (no format specifiers).
getchar(), putchar(): Read/write single characters.
gets(), puts(): Read/write strings.
Eg)
21. a) Why is the main() function considered the entry point of a C program?
(3 Marks)
· Execution starts from main() – The operating system calls main() when a
C program runs.
· Controls program flow – Other functions execute within main().
· Standardization – main() is required in every C program.
b) Write a C program that accepts the user's name and age as input and
prints a message in the
format: Hello <name>, you are <age> years old. (6 Marks)
22. Describe the usage of the built-in functions scanf() for input and printf() for
output formatting.(3)
23. What is the role of the documentation section in a C program?(3)
· Provides comments about the program (author, purpose, date).
· Improves code readability for other developers.
· Uses /*...*/ or // for documentation.
24. What is the basic structure of a C program, and why is it important to
include elements like the main function and header files?(3)
Why Important?
Header Files (#include <stdio.h>) – Enable built-in functions.
main() Function – Essential entry point.
Code Blocks ({}) – Define execution scope.
Without main(), a C program cannot execute!
25. a) What are nested if statements? Explain their importance with an
example. (4 Marks)
A nested if statement is an if condition inside another if condition. It is used
when multiple conditions need to be checked sequentially.
Importance:
Allows checking multiple conditions step by step.
Used when decisions depend on previous conditions.
b) Write a program to check whether a number is even or odd. (5 Marks)
26. a) Write a program to check whether the candidate’s age is greater
than 17. If yes, display “Eligible to Vote.” (5 Marks)
b) Write a C program to demonstrate the use of if and if-else statements
with examples. (4 Marks)
27. a) Write a program to check whether the entered number is less than
10. If yes, display the text “OK.” (5 Marks)
b) What is a simple if statement? Explain their importance with an
example. (4 Marks)
A simple if statement executes a block of code only if the condition is true.
Importance:
Helps control the flow of execution.
Ensures that only relevant code executes based on conditions.
28. Write a simple C program that checks whether a number is positive
or negative using if statement.
29. Write a C program to find the smallest number among three numbers.
30. Explain the difference between if and if-else statements in C.
31. Explain the role and functionality of the switch statement in C with an
example.(9)
The switch statement in C is a control structure used to simplify decision-
making based on the value of an expression.
It provides a more organized way to handle multiple conditions than a series
of if...else statements.
Eg)
Importance:
Simplifies decision-making for multiple cases.
Improves performance over multiple if-else checks.
Works with integer and character values.
32. Write a C program to check whether a given number is an Armstrong
number.(9)
33. Write a menu-driven program to perform addition, subtraction,
multiplication, and division.(9)
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
while (1) {
printf("\nMenu:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 5) {
printf("Exiting program.\n");
break;
}
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case 2:
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case 3:
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case 4:
if (num2 != 0)
result = num1 / num2;
else
printf("Error: Division by zero.\n");
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
34. Can a switch statement work with float or double data types? Why or why
not?(3)
No, the switch statement cannot work with float or double data types because:
1. Floating-point numbers are stored as approximations (due to
precision errors).
2. switch requires discrete (exact match) case labels, which floating-
point values cannot reliably provide.
34. Differentiate between while and do-while statements.(3)
36. Compare an exit-controlled loop and an entry-controlled loop with suitable
examples.(3)
37. Write a program to generate the following pattern:(9)
1
12
123
1234
37. a) Write a C program to display the Fibonacci series up to a given number
of terms. (7 Marks)
b) Why is a loop necessary for generating the Fibonacci sequence in a
program? (2 Marks)
A loop is necessary because each new term in the Fibonacci sequence is the
sum of the two preceding terms. This repetitive calculation is efficiently
handled by loops, avoiding redundant code.
38. Write a program to find the sum of digits of a given number.(3)
40. Demonstrate the use of nested loops in C with an example.(3)
Explanation:
The outer loop runs 3 times.
For each iteration of the outer loop, the inner loop runs 3 times.
This results in a grid of coordinate pairs.
41. Write a for loop to print all even numbers between 1 and 20.(3)
42. Explain the structure of a for loop and the significance of its three
components.
43. a) Write a C program to display the corresponding day of the week using
switch statements. (5 Marks)
b) Explain how the break statement works in a switch statement. (4 Marks)
· The break statement terminates the current case in a switch block.
· Without break, the program will continue to execute the subsequent cases
even if the condition is met (this is called fall-through).
· It helps prevent unintended code execution after the desired case.
44. Write a C program to check whether a number is a palindrome.(9)
45. Write a program to input marks for three subjects, calculate the total
percentage, and display grades according to the following criteria:(9)
Percentage >= 90: A grade
Percentage >= 80: B grade
Percentage >= 70: C grade
Percentage < 70: Fail.
46. Write a C program to find the sum of the first and last digits of a number.(3)
47. Write a C program to display a multiplication table of a given number.(3)
48. Differentiate between break and continue statements with examples.(3)