C Programming - Answer Sheet (Subject Code: 29715)
Q.1
A. Algorithm to find roots of a quadratic equation:
1. Start
2. Input a, b, c
3. Calculate discriminant D = b^2 - 4ac
4. If D > 0, roots are real and different: (-bsqrtD)/2a
5. If D = 0, roots are real and same: -b/2a
6. If D < 0, roots are complex: -b/2a sqrt|D|/2a i
7. Stop
Flowchart: [Diagram Required]
B. C Program to find frequency of characters:
#include <stdio.h>
#include <string.h>
int main() {
char str[100]; int freq[256] = {0};
printf("Enter a string: "); gets(str);
for(int i = 0; str[i]; i++) freq[(int)str[i]]++;
for(int i = 0; i < 256; i++) if(freq[i] > 0) printf("%c: %d\n", i, freq[i]);
return 0;
C. Structure vs Union:
- Structure allocates memory for all members; Union allocates memory for the largest member.
- All members of structure can be used at once; only one member of union at a time.
- Structure size sum of member sizes; Union size = largest member size.
D. Leap year check:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: "); scanf("%d", &year);
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
E. Logical operators in C:
1. && (Logical AND)
2. || (Logical OR)
3. ! (Logical NOT)
Used in conditions and boolean expressions.
Q.2
A. Recursion is when a function calls itself to solve a smaller instance of a problem.
Factorial using recursion:
int fact(int n) { return (n <= 1) ? 1 : n * fact(n-1); }
B. while loop checks condition before execution; do-while checks after.
while: entry-controlled
do {
// code
} while (condition);
C. Basic Data Types in C:
int (4 bytes), float (4 bytes), char (1 byte), double (8 bytes)
Ranges:
int: -2,147,483,648 to 2,147,483,647
char: -128 to 127
float: 1.2E-38 to 3.4E+38
Q.3
A. User-defined functions:
strlen - counts characters until null.
strcat - appends second string to first.
B. Bitwise Operators:
& (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
Example:
5 & 3 = 1, 5 | 3 = 7
C. Pattern printing:
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter lines: "); scanf("%d", &n);
for(i=0; i<n; i++) {
for(j=0; j<=i; j++) printf("%c ", 'A'+i);
printf("\n");
return 0;
Q.4
A. Switch Case vs if-else:
Switch checks one variable; if-else can check multiple conditions.
Switch is cleaner when checking equality.
B. Smallest in array:
#include <stdio.h>
int main() {
int n, a[100], min;
printf("Enter N: "); scanf("%d", &n);
for(int i=0;i<n;i++) scanf("%d", &a[i]);
min = a[0];
for(int i=1;i<n;i++) if(a[i] < min) min = a[i];
printf("Smallest: %d", min);
return 0;
C. Palindrome check:
#include <string.h>
int main() {
char str[100]; int i, len, flag = 1;
gets(str); len = strlen(str);
for(i=0;i<len/2;i++) if(str[i] != str[len-i-1]) { flag = 0; break; }
printf(flag ? "Palindrome" : "Not Palindrome");
return 0;
Q.5
A. Patient record program:
Use structure array to store details and loop to print matching disease.
B. Function Prototype ensures type checking before use.
Example:
int sum(int, int);
int main() { sum(2,3); }
int sum(int a, int b) { return a + b; }
C. Entry-controlled: while, for
Exit-controlled: do-while
In entry, condition checked first; exit, condition checked after.
Q.6
A. Transpose of matrix:
for(i=0;i<row;i++) for(j=0;j<col;j++) transpose[j][i] = matrix[i][j];
B. Star pattern:
**
***
Code uses nested loops with spaces and stars.
C. Algorithm: Step-by-step procedure to solve a problem.
Properties:
1. Input
2. Output
3. Definiteness
4. Finiteness
5. Effectiveness