c Program Model Solved
c Program Model Solved
PARTA
Answer all questions. Each question carries 3 marks CO Marks
1 Convert the below if-else to conditional operator 1 (3)
if (first_check)
{
"access denied";
}
else
{
if (second_check)
{
"access denied";
}
else
{
"access granted";
}
}
2 State proper reasons to identify whether the given identifiers are valid or not. 1 (3)
i) 9marks
ii) Mark 2020
iii) v4vote
iv) _csdept
v) @cse
vi) AxB
3 Explain the different ways in which you can declare and initialize a single- 2 (3)
dimensional array.
4 How can a list of strings be stored within a two-dimensional array? How can the 2 (3)
individual strings be processed? What library functions are available to simplify
string processing?
5 Suppose function F1 calls function F2 within a C program. Does the order of the 3 (3)
function definitions make any difference? Explain.
7 Suppose a function receives a pointer as an argument. Explain how the function 4 (3)
3
prototype is written. In particular, explain how the data type of the pointer argument
is represented.
8 While using the statement, fp = fopen( "myfile.c", "r" ) ; what happens if, 5 (3)
− ‘myfile.c’ does not exist on the disk
− ‘myfile.c’ exists on the disk
PARTB
Answer any one full question from each module. Each question carries 9 marks
Module 1
9 a 1 (5)
Write a C Program to check if a given number is a strong number or not.
A strong number is a number in which the sum of the factorial of the digits is equal
to the number itself. Eg:- 145=1!+4!+5!=1+24+120=145
b Write a C program that calculates and prints the result of the following expression: 1 (4)
result = 5 * (6 + 2) / 4 - 3. Explain how parentheses affect the precedence of
operators in this expression.
10 a Write a C program that reads employee's ID, name, status (‘F’ for full-time and ‘P’ 1 (6)
for part-time) and basic pay. DA and HRA for full-time employees are 75% and
10% of basic pay respectively. Part-time employees have basic pay only. Print the
total salary of the employee.
b 1 (3)
Explain the difference between primitive (basic) data types and derived data types
in C. Provide examples for each category. Why is it important to choose the
correct data type for variables in a program?
Module 2
11 a Write a C program to reverse the content of an array without using another array. 2 (4)
b Given a list of cities, their literacy level, and the population, write a program to print 2 (5)
the list cities sorted lexicographically. Assume duplication in city names.. Break the
ties with the value of literacy level, and then the population.
18
a Write a C program to rotate (re-insert rightmost element at the left) elements of an 2 (4)
12 array to the right by n-steps.
b Write a C program that allows the user to enter a line of text, store it in an array, 2 (5)
and then display it backward. The line length should be unspecified (terminated by
pressing the Enter key), but assume it will not exceed 80 characters.
Module 3
13 a Develop a function that takes the two dates as input and compares them. The 3 (4)
function should return 1 if date1 is earlier than date2, return 0 if date1 is later than
date2. Write code to display whether date1 is earlier, later, or the same as date2
based on the function’s result.
b Define a structure named Vehicle with members model (character array), year 2 (5)
(integer), and price (float). Create an array of 10 Vehicle structures and write a
function to accept user input for each vehicle's details and Sort the array in
descending order based on price.
14 a Define a structure that can describe a hotel with the following members: name, 3 (4)
address, grade, average room charge, and number of rooms. Write a function to
print out hotels of a given grade, sorted in order of their average room charges.
b Write a function to print out hotels with an average room charge less than a 3 (5)
specified value in the above problem.
3
scan to join our watsapp group
Module 4
15 a Write a c program snippet to open a file in write mode and check if the file opened 5 (4)
successfully. if it fails ,print an error message and terminate the program.
b Two persons want to access a file "sample.txt". First person want to read the data 5 (5)
from the file. The second person want to read and write the data from and to the
file simultaneously. Can you help them to do so by writing the corresponding
programming codes?
16 a Write a function calculates the roots of a quadratic equation with coefficients a, b, 4 (4)
and c (passed as pointer parameters).
b In a small firm, employee numbers are given in serial numerical order, that is 1, 2, 5 (5)
3, etc. − Create a file of employee data with following information: employee
number, name, sex, gross salary. − If more employees join, append their data to the
file. − If an employee with serial number 25 (say) leaves, delete the record by
making gross salary 0. − If some employee’s gross salary increases, retrieve the
record and update the salary. Write a program to implement the above operations.
*****
3
Explanation of All Answers for
the C Programming Question
Paper
PART A scan to join our watsapp group
Explanation: The nested if-else is converted to a ternary conditional operator. The first condition
checks first_check, and if true, returns "access denied". If false, it checks second_check in
another nested ternary operation.
Explanation: C identifiers must start with a letter or underscore, can contain letters, digits, and
underscores, and cannot contain spaces or special characters (except underscore).
Explanation: Arrays can be declared with or without initialization, with explicit or implicit size,
and can be partially initialized.
Explanation: Each string occupies one row in the 2D array. Columns must accommodate the
longest string + null terminator. Standard string functions simplify processing.
Explanation: Variables declared outside all functions have static storage duration and file scope.
They're initialized to zero by default if not explicitly initialized.
Explanation: The data type before the asterisk indicates what type of data the pointer points to.
This ensures type safety in pointer operations.
If 'myfile.c' doesn't exist: fopen() returns NULL (opening in "r" mode fails for non-
existent files)
Explanation: "r" mode is for reading existing files. Always check the return value of fopen() to
handle errors.
PART B
Module 1
Question 9a: Strong number program
#include <stdio.h>
int factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n-1);
while(temp) {
temp /= 10;
}
return sum == num;
int main() {
int num;
scanf("%d", &num);
return 0;
int main() {
float result = 5 * (6 + 2) / 4 - 3;
return 0;
Explanation: Parentheses have highest precedence, so (6+2) is evaluated first (8). Then
multiplication and division (left to right): 5*8=40, 40/4=10. Finally subtraction: 10-3=7.
int main() {
if(status == 'F') {
} else {
total = basic;
return 0;
Module 2
Question 11a: Reverse array in place
void reverseArray(int arr[], int size) {
arr[i] = arr[size-1-i];
arr[size-1-i] = temp;
}
}
#include <string.h>
struct City {
char name[50];
float literacy;
int population;
};
cities[j] = cities[j+1];
cities[j+1] = temp;
}
Question 12a: Rotate array right by n steps
void rotateRight(int arr[], int size, int n) {
n = n % size;
arr[i] = arr[i-1];
arr[0] = temp;
#include <string.h>
int main() {
char text[80];
text[i] = text[len-1-i];
text[len-1-i] = temp;
return 0;
Module 3
Question 13a: Date comparison
struct Date {
};
#include <string.h>
struct Vehicle {
char model[50];
int year;
float price;
};
vehicles[j+1] = temp;
char name[50];
char address[100];
char grade;
float avgRoomCharge;
int numRooms;
};
hotels[j] = hotels[j+1];
hotels[j+1] = temp;
if(hotels[i].grade == grade) {
Module 4
Question 15a: File open check
#include <stdio.h>
#include <stdlib.h>
int main() {
if(fp == NULL) {
exit(EXIT_FAILURE);
fclose(fp);
return 0;
Copy
Download
if(discriminant > 0) {
} else if(discriminant == 0) {
} else {
printf("Complex roots\n");
#include <stdlib.h>
struct Employee {
int number;
char name[50];
char sex;
float grossSalary;
};
fseek(fp, 0, SEEK_END);
fseek(fp, 0, SEEK_SET);
if(emp.number == empNo) {
emp.grossSalary = 0;
break;
fseek(fp, 0, SEEK_SET);
if(emp.number == empNo) {
emp.grossSalary = newSalary;
fseek(fp, -sizeof(struct Employee), SEEK_CUR);
break;