SYSTEM PROGRAMMING QUESTIONS
1. Read and Print a String
Question: Write a C program that reads a string from the user and prints it.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("You entered: %s", str);
return 0;
}
2. Read and Sum Two Integers
Question: Write a C program that reads two integers from the user and prints their sum.
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
3. Read and Print an Array of Integers
Question: Write a C program that reads an array of integers from the user and prints the array.
#include <stdio.h>
int main() {
int n, i;
int arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
4. Read a File and Print Its Content
Question: Write a C program that reads a text file and prints its content to the screen.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[100], c;
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
c = fgetc(fptr);
while (c != EOF) {
printf("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
5. Write and Read from a File
Question: Write a C program that writes user input to a file and then reads from the file and
prints its content.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[100], str[100];
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "w");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
printf("Enter a string to write to the file: ");
getchar(); // consume newline left by scanf
fgets(str, 100, stdin);
fprintf(fptr, "%s", str);
fclose(fptr);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
printf("Content of the file:\n");
while (fgets(str, 100, fptr) != NULL) {
printf("%s", str);
}
fclose(fptr);
return 0;
}
6. Append to a File
Question: Write a C program that appends user input to an existing file.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[100], str[100];
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "a");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
printf("Enter a string to append to the file: ");
getchar(); // consume newline left by scanf
fgets(str, 100, stdin);
fprintf(fptr, "%s", str);
fclose(fptr);
printf("Content appended to the file.\n");
return 0;
}
7. Count Words in a File
Question: Write a C program that counts the number of words in a text file.
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fptr;
char filename[100], c;
int word_count = 0, in_word = 0;
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
while ((c = fgetc(fptr)) != EOF) {
if (isspace(c)) {
in_word = 0;
} else if (!in_word) {
in_word = 1;
word_count++;
}
}
fclose(fptr);
printf("Word count: %d\n", word_count);
return 0;
}
8. Read and Print a Matrix
Question: Write a C program that reads a matrix from the user and prints it.
#include <stdio.h>
int main() {
int rows, cols, i, j;
int matrix[10][10];
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements of the matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("The matrix is:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
9. Calculate Average of Numbers
Question: Write a C program that reads a list of numbers from a file and calculates their average.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[100];
int num, count = 0;
double sum = 0.0;
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
while (fscanf(fptr, "%d", &num) != EOF) {
sum += num;
count++;
}
fclose(fptr);
if (count == 0) {
printf("No numbers found in the file.\n");
} else {
printf("Average: %.2f\n", sum / count);
}
return 0;
}
10. Reverse Content of a File
Question: Write a C program that reads the content of a file and prints it in reverse order.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fptr;
char filename[100];
long file_size;
char *buffer;
printf("Enter the filename: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 1;
}
fseek(fptr, 0, SEEK_END);
file_size = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
buffer = (char*)malloc(file_size + 1);
fread(buffer, 1, file_size, fptr);
buffer[file_size] = '\0';
fclose(fptr);
printf("Reversed content:\n");
for (long i = file_size - 1; i >= 0; i--) {
printf("%c", buffer[i]);
}
printf("\n");
free(buffer);
return 0;
}
Question 11
Write a program to read an integer from the user and print it.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Question 12
Write a program to read a floating-point number from the user and print it with 2 decimal
places.
#include <stdio.h>
int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num);
printf("You entered: %.2f\n", num);
return 0;
}
Question 13
Write a program to read a string (up to 100 characters) from the user and print it.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
Question 14
Write a program to read two integers from the user and print their sum.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("Sum: %d\n", num1 + num2);
return 0;
}
Question 15
Write a program to read a line of text from the user and print it.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a line of text: ");
fgets(str, sizeof(str), stdin);
printf("You entered: %s", str);
return 0;
}
Question 16
Write a program to read an integer array of size 5 from the user and print the elements.
#include <stdio.h>
int main() {
int arr[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("You entered: ");
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Question 17
Write a program to read a file name from the user, open the file, and print its contents.
#include <stdio.h>
int main() {
char filename[100];
char ch;
FILE *file;
printf("Enter the file name: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
Question 18
Write a program to write a string entered by the user to a file.
#include <stdio.h>
int main() {
char str[100];
FILE *file;
printf("Enter a string: ");
scanf("%s", str);
file = fopen("[Link]", "w");
if (file == NULL) {
printf("Could not open file for writing\n");
return 1;
}
fprintf(file, "%s", str);
fclose(file);
printf("String written to [Link]\n");
return 0;
}
Question 19
Write a program to read integers from a file and print their sum.
#include <stdio.h>
int main() {
FILE *file;
int num, sum = 0;
file = fopen("[Link]", "r");
if (file == NULL) {
printf("Could not open file [Link]\n");
return 1;
}
while (fscanf(file, "%d", &num) != EOF) {
sum += num;
}
fclose(file);
printf("Sum of integers in the file: %d\n", sum);
return 0;
}
Question 20
Write a program to read a character from the user and determine if it is a vowel or
consonant.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}
return 0;
}
Question 21
Write a program that reads a string from the user and prints it back to the user.
Solution:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("You entered: %s", str);
return 0;
}
Question 22
Write a program that reads an integer from the user and checks if it is even or odd.
Solution:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
return 0;
}
Question 23
Write a program that reads two floating-point numbers and prints their sum.
Solution:
#include <stdio.h>
int main() {
float num1, num2, sum;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
sum = num1 + num2;
printf("Sum: %.2f\n", sum);
return 0;
}
Question 24
Write a program that reads a file and prints its contents to the console.
Solution:
#include <stdio.h>
int main() {
FILE *file;
char filename[100], ch;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
Question 25
Write a program that writes "Hello, World!" to a file.
Solution:
#include <stdio.h>
int main() {
FILE *file;
char filename[100] = "[Link]";
file = fopen(filename, "w");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
printf("Data written to file %s\n", filename);
return 0;
}
Question 26
Write a program that reads a line of text and counts the number of vowels in it.
Solution:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, count = 0;
printf("Enter a string: ");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (tolower(str[i]) == 'a' || tolower(str[i]) == 'e' ||
tolower(str[i]) == 'i' ||
tolower(str[i]) == 'o' || tolower(str[i]) == 'u') {
count++;
}
}
printf("Number of vowels: %d\n", count);
return 0;
}
Question 27
Write a program that reads three integers and prints the largest one.
Solution:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
int largest = num1;
if (num2 > largest) largest = num2;
if (num3 > largest) largest = num3;
printf("The largest number is: %d\n", largest);
return 0;
}
Question 28
Write a program that reads a string and prints its reverse.
Solution:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i;
printf("Enter a string: ");
fgets(str, 100, stdin);
len = strlen(str);
printf("Reversed string: ");
for (i = len - 1; i >= 0; i--) {
putchar(str[i]);
}
return 0;
}
Question 29
Write a program that reads integers until the user enters a negative number and then
prints the sum of all entered numbers (excluding the negative one).
Solution:
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter integers (negative number to end): ");
while (1) {
scanf("%d", &num);
if (num < 0) break;
sum += num;
}
printf("Sum of entered numbers: %d\n", sum);
return 0;
}
Question 30
Write a program that reads a file and counts the number of lines in it.
Solution:
#include <stdio.h>
int main() {
FILE *file;
char filename[100], ch;
int lines = 0;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
while ((ch = fgetc(file)) != EOF) {
if (ch == '\n') lines++;
}
fclose(file);
printf("Number of lines: %d\n", lines);
return 0;
}
31. Read a file and count the number of words.
Question: Write a program that reads a text file and counts the number of words in it. Assume
words are separated by whitespace.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: wordcount [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
int words = 0;
int in_word = 0;
char c;
while ((c = fgetc(fpt)) != EOF) {
if (isspace(c)) {
if (in_word) {
words++;
in_word = 0;
}
} else {
in_word = 1;
}
}
if (in_word) words++; // Count the last word if it exists
fclose(fpt);
printf("Number of words: %d\n", words);
return 0;
}
32. Convert text to uppercase.
Question: Write a program that reads a text file and converts all the text to uppercase, then prints
the result to the console.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: toupper [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
char c;
while ((c = fgetc(fpt)) != EOF) {
putchar(toupper(c));
}
fclose(fpt);
return 0;
}
33. Read numbers from a file and calculate the average.
Question: Write a program that reads a series of integers from a file and calculates their average.
Assume the numbers are separated by whitespace.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: average [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
int num, count = 0;
double sum = 0.0;
while (fscanf(fpt, "%d", &num) == 1) {
sum += num;
count++;
}
fclose(fpt);
if (count == 0) {
printf("No numbers found.\n");
} else {
printf("Average: %.2f\n", sum / count);
}
return 0;
}
34. Copy contents of one file to another.
Question: Write a program that copies the contents of one file to another file.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: copy [sourcefile] [destfile]\n");
exit(0);
}
FILE *src, *dest;
if ((src = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
if ((dest = fopen(argv[2], "w")) == NULL) {
printf("Unable to open %s for writing\n", argv[2]);
fclose(src);
exit(0);
}
char c;
while ((c = fgetc(src)) != EOF) {
fputc(c, dest);
}
fclose(src);
fclose(dest);
return 0;
}
35. Read and print each line of a file.
Question: Write a program that reads a text file line by line and prints each line to the console.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_LENGTH 1000
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: printlines [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), fpt) != NULL) {
printf("%s", line);
}
fclose(fpt);
return 0;
}
36. Count the number of lines in a file.
Question: Write a program that counts the number of lines in a text file.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: countlines [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
int lines = 0;
char c;
while ((c = fgetc(fpt)) != EOF) {
if (c == '\n') {
lines++;
}
}
fclose(fpt);
printf("Number of lines: %d\n", lines);
return 0;
}
37. Append text to a file.
Question: Write a program that appends text provided by the user to the end of a file.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: append [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "a")) == NULL) {
printf("Unable to open %s for appending\n", argv[1]);
exit(0);
}
char text[1000];
printf("Enter text to append (end with Ctrl+D):\n");
while (fgets(text, sizeof(text), stdin) != NULL) {
fputs(text, fpt);
}
fclose(fpt);
return 0;
}
38. Replace all instances of a word in a file.
Question: Write a program that reads a file and replaces all instances of a specified word with
another word, then writes the modified content to a new file.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1000
void replace_word(const char *src, const char *word, const char *replacement,
char *result) {
char *pos, temp[MAX_LINE_LENGTH];
int len = strlen(word);
while ((pos = strstr(src, word)) != NULL) {
strncpy(temp, src, pos - src);
temp[pos - src] = '\0';
sprintf(temp + (pos - src), "%s%s", replacement, pos + len);
strcpy(src, temp);
}
strcpy(result, src);
}
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: replace [filename] [word] [replacement]\n");
exit(0);
}
FILE *fpt, *new_fpt;
if ((fpt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
char new_filename[100];
snprintf(new_filename, sizeof(new_filename), "new_%s", argv[1]);
if ((new_fpt = fopen(new_filename, "w")) == NULL) {
printf("Unable to open %s for writing\n", new_filename);
fclose(fpt);
exit(0);
}
char line[MAX_LINE_LENGTH], result[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), fpt) != NULL) {
replace_word(line, argv[2], argv[3], result);
fputs(result, new_fpt);
}
fclose(fpt);
fclose(new_fpt);
return 0;
}
39. Read and print binary file content.
Question: Write a program that reads the contents of a binary file and prints the hexadecimal
representation of its content to the console.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: readbinary [filename]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "rb")) == NULL) {
printf("Unable to open %s for reading\n", argv[1]);
exit(0);
}
unsigned char buffer[16];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), fpt)) > 0) {
for (size_t i = 0; i < bytes_read; i++) {
printf("%02X ", buffer[i]);
}
printf("\n");
}
fclose(fpt);
return 0;
}
40. Write a formatted log entry to a file.
Question: Write a program that appends a formatted log entry to a log file. The log entry should
include a timestamp and a user-provided message.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void get_current_time(char *buffer, size_t size) {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", timeinfo);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: logentry [logfile]\n");
exit(0);
}
FILE *fpt;
if ((fpt = fopen(argv[1], "a")) == NULL) {
printf("Unable to open %s for appending\n", argv[1]);
exit(0);
}
char timestamp[20];
char message[1000];
get_current_time(timestamp, sizeof(timestamp));
printf("Enter log message: ");
fgets(message, sizeof(message), stdin);
fprintf(fpt, "[%s] %s", timestamp, message);
fclose(fpt);
return 0;
}