C Programming Exam Solutions
C Programming Exam Solutions
Header files in C (like stdio.h, math.h) contain function declarations, macros, and constants
used in programs. They allow code reuse and help manage complex programs by separating
function declarations from their definitions.
Macros are preprocessor directives expanded at compile-time, while functions are compiled
and executed at runtime. Macros are faster but error-prone; functions offer type checking
and debugging ease.
Structure is a user-defined data type that groups variables of different types. In a structure,
all members occupy separate memory locations. In a union, all members share the same
memory, so only one can be used at a time.
getc(): Reads a character from a file. getchar(): Reads from standard input. getch(): Reads
without echoing character. getche(): Reads and echoes character (used in conio.h).
```c
#include <stdio.h>
int main() {
int a[20], i, j, temp;
printf("Enter 20 integers:\n");
for(i = 0; i < 20; i++)
scanf("%d", &a[i]);
for(i = 0; i < 19; i++)
for(j = i + 1; j < 20; j++)
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("Sorted numbers:\n");
for(i = 0; i < 20; i++)
printf("%d ", a[i]);
return 0;
}
```
```c
#include <stdio.h>
int main() {
char str[100];
int i, len = 0, isPalindrome = 1;
printf("Enter a string: ");
scanf("%s", str);
while (str[len] != '\0') len++;
for(i = 0; i < len/2; i++) {
if(str[i] != str[len-1-i]) {
isPalindrome = 0;
break;
}
}
if(isPalindrome)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
```
```c
#include <stdio.h>
struct Complex {
float real, imag;
};
struct Complex add(struct Complex a, struct Complex b) {
struct Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}
int main() {
struct Complex c1 = {2.3, 4.5}, c2 = {1.6, 2.5}, sum;
sum = add(c1, c2);
printf("Sum = %.1f + %.1fi\n", sum.real, sum.imag);
return 0;
}
```
```c
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *ptr = &arr[0][0];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", *(ptr + i*3 + j));
}
printf("\n");
}
return 0;
}
```
```c
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *f = fopen("test1.txt", "r");
char ch;
int words = 0, chars = 0, inWord = 0;
if (!f) return 1;
while ((ch = fgetc(f)) != EOF) {
chars++;
if (isspace(ch)) inWord = 0;
else if (!inWord) {
words++;
inWord = 1;
}
}
fclose(f);
printf("Words: %d, Characters: %d\n", words, chars);
return 0;
}
```
```c
#include <stdio.h>
int main() {
int a[4][4], b[4][4], c[4][4] = {0};
printf("Enter first 4x4 matrix:\n");
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
printf("Enter second 4x4 matrix:\n");
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
scanf("%d", &b[i][j]);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
for(int k = 0; k < 4; k++)
c[i][j] += a[i][k] * b[k][j];
printf("Result matrix:\n");
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
```
```c
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
char author[50];
float price;
};
int main() {
struct Book books[3];
int i, max = 0, min = 0;
for(i = 0; i < 3; i++) {
printf("Enter title, author, price of book %d:\n", i+1);
scanf(" %[^
]%*c %[^
]%*c %f", books[i].title, books[i].author, &books[i].price);
}
for(i = 1; i < 3; i++) {
if(books[i].price > books[max].price)
max = i;
if(books[i].price < books[min].price)
min = i;
}
printf("Most Expensive Book: %s by %s ($%.2f)\n", books[max].title, books[max].author,
books[max].price);
printf("Lowest Priced Book: %s by %s ($%.2f)\n", books[min].title, books[min].author,
books[min].price);
return 0;
}
```