C QPA - 2024
C QPA - 2024
C QPA - 2024
Max. Marks: 60
Time: 2½ Hours
SECTION – A
Use Often used for single-key input in programs Suitable for standard input
Case like games. operations.
#define PI 3.14
const int AGE = 25;
Types of constants:
Example:
Example:
int a = 10;
int *ptr = &a; // Pointer stores the address of 'a'
printf("Address: %p, Value: %d", ptr, *ptr);
SECTION – B
10. What are the rules to be followed while constructing a variable? Give examples.
Example:
Example:
● Advantages:
○ Stores multiple elements of the same type in a contiguous memory block.
○ Easy to iterate using loops.
● Disadvantages:
○ Fixed size, cannot dynamically expand.
○ Can only store one data type.
Memory Allocates memory for all members. Shares memory between members.
Access All members can be accessed at once. Only one member at a time.
Example:
● Advantages:
○ Dynamic memory allocation.
○ Access arrays and functions efficiently.
● Disadvantages:
○ Misuse can lead to segmentation faults.
○ Harder to debug.
SECTION – C
●
Example:
char 1
int 4
float 4
double 8
long int 4 or 8
short int 2
17. a) Explain nested if with an example.
A nested if is an if statement inside another if statement, used to check multiple conditions.
Syntax:
if (condition1) {
if (condition2) {
// Code block
}
}
Example:
17. b) Write a program to find the sum of the first N natural numbers.
Code:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Add each natural number
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Output Example:
For N = 5: The program will output 15.
18. a) Explain two-dimensional arrays.
A two-dimensional (2D) array is an array of arrays, often visualized as a table with rows and
columns. It is useful for storing matrices or tabular data.
Syntax:
data_type array_name[rows][columns];
Example:
int mat[2][3] = {
{1, 2, 3}, // Row 1
{4, 5, 6} // Row 2
};
printf("%d", mat[0][1]); // Accesses element at row 0, column 1 (Output: 2)
18. b) Write a C program to read, display, and find the trace of a square matrix.
The trace of a matrix is the sum of its diagonal elements.
Code:
#include <stdio.h>
int main() {
int n, trace = 0;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int mat[n][n];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// Calculate trace
for (int i = 0; i < n; i++) {
trace += mat[i][i]; // Add diagonal elements
}
Code:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, D, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
D = b * b - 4 * a * c; // Discriminant
if (D >= 0) {
root1 = (-b + sqrt(D)) / (2 * a);
root2 = (-b - sqrt(D)) / (2 * a);
printf("Roots are: %.2f and %.2f\n", root1, root2);
} else {
printf("Roots are imaginary.\n");
}
return 0;
}
void greet() {
printf("Hello, World!");
}
int getValue() {
return 10;
}