Problem Solving
Problem Solving
triangular matrix or not. An upper triangular matrix is the one in which all the elements
below its principal diagonal are zero.
#include<stdio.h>
int main() {
int M, N, i, j;
int isUpper = 1;
scanf("%d", &matrix[i][j]);
}
}
for (i = 1; i < M; i++) {
for (j = 0; j < i && j < N; j++) { if (matrix[i][j] != 0) {
isUpper = 0; break;
}
}
if (!isUpper)
break;
}
if (isUpper)
printf("The matrix is an upper triangular matrix.\n");
else
printf("The matrix is NOT an upper triangular matrix.\n");
return 0;
}
Problem 2 : Write a C program to read two square matrices of non-zero elements of order M
X M and then subtract the elements of the second matrix from the first matrix. Then display
the resultant matrix to the console.
#include <stdio.h>
int main() {
int m;
printf("Enter the order (m) of the square matrices: "); scanf("%d", &m);
if (m <= 0) {
scanf("%d", &matrix1[i][j]);
}
}
#include <stdio.h>
#include <string.h>
int main() {
char names[MAX_NAMES][MAX_LENGTH], search[MAX_LENGTH];
int n, found = 0;
return 0;
}
Problem 4 : Develop a C program to read two square binary matrices of order N x N from
the user and perform the XOR on the elements of the matrices and print the resultant matrix
to the output screen.
#include <stdio.h>
#define MAX 100
int main() {
int matrix1[MAX][MAX], matrix2[MAX][MAX], result[MAX][MAX];
int N;
printf("Enter the order of the square matrices (N x N): ");
scanf("%d", &N);
if (N <= 0 || N > MAX) {
printf("Invalid matrix size. Please use 1 to %d.\n", MAX); return 1;}
printf("Enter elements of first %d x %d binary matrix (0 or 1):\n", N, N); for (int i = 0; i < N;
i++) {
for (int j = 0; j < N; j++) { scanf("%d", &matrix1[i][j]);
if (matrix1[i][j] != 0 && matrix1[i][j] != 1) {
printf("Invalid input. Only binary values (0 or 1) are allowed.\n");
return 1;
}
}}
printf("Enter elements of second %d x %d binary matrix (0 or 1):\n", N, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &matrix2[i][j]);
if (matrix2[i][j] != 0 && matrix2[i][j] != 1) {
printf("Invalid input. Only binary values (0 or 1) are allowed.\n"); return 1;
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = matrix1[i][j] ^ matrix2[i][j];
}
}
printf("Resultant matrix after XOR operation:\n"); for (int i = 0; i < N; i++) {
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char sentence[1000]; printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin); for (int i = 0; sentence[i] != '\0'; i++) {
char ch = tolower(sentence[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
sentence[i] = 'X';
}
}
printf("Modified sentence: %s\n", sentence);
return 0;
}
Problem 7 : Develop a C program to compare two strings using a UDF and return 1 if the
strings are equal and 0 otherwise without using the built-in function strcmp(). Display an
appropriate message to the output screen.
Sample Input:
Enter two strings Welcome welcome
Output:
The Strings are not equal.
#include <stdio.h>
#include <string.h>
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0;
}
i++;
}
return (str1[i] == '\0' && str2[i] == '\0');
}
int main() {
char str1[100], str2[100];
if (compareStrings(str1, str2)) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Problem 8 : Develop a C program to find the number of occurrences of each alphabet in a
string accepted from the user and display the same to the output screen. Assume that the
string contains only alphabets.
#include <stdio.h>
#include <ctype.h> // for tolower()
int main() {
char str[1000];
int count[26] = {0};
return 0;
}
problem 9 : Design a UDF in C to find the product and quotient of two numbers passed to it
from the calling program. Display the product and the quotient in the main program. Read the
two numbers in the main program & also display their product and quotient. Implement using
a pointer.
#include <stdio.h>
void calculateProductAndQuotient(float a, float b, float *product, float *quotient);
int main() {
float num1, num2; float product, quotient;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
return 1;
}
calculateProductAndQuotient(num1, num2, &product, "ient);
printf("Product: %.2f\n", product);
printf("Quotient: %.2f\n", quotient);
return 0;
}
void calculateProductAndQuotient(float a, float b, float *product, float *quotient) {
*product = a * b;
*quotient = a/b;
}
Problem 10 : Write a C program to accept N integers from the user into an array. If any
integer is positive then subtract 2 from it, if integer is negative add 1 to it and if zero then do
nothing. Implement a UDF using a pointer to perform these operations and finally display the
modified array to the screen in the calling program.
Sample Input:
1 3 -5 0 8 -4 -1
Output:
-1 1 -4 0 6 -3 0
#include <stdio.h>
Sample Input/Output:
Input:
Enter a String: Rotator
Output:
Rotator is a palindrome
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
if (isPalindrome(str)) {
printf("The string \"%s\" is a palindrome.\n", str);
} else {
printf("The string \"%s\" is not a palindrome.\n", str);
}
return 0;
}
Problem 12 : Design a UDF that returns the count of special characters present in a sentence
passed to it using pointer to the calling program. Implement a C program to read a string in
the main program and display all the count of the special characters returned by the function
to the console.
Sample Input:
Enter a String: Rotation%# is$? ?rotating $motion?
Output:
Special Character Count: 10
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char sentence[1000];
int specialCharCount;
return 0;
}
Problem 13 : Write a C program to dynamically allocate memory for an integer array of size
N, allow the user to input N integers, resize the array to N+M using realloc to add M more
integers, compute the sum of all N+M integers, and properly release the allocated memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int N, M, sum = 0;
int newSize = N + M;
int *temp = realloc(arr, newSize * sizeof(int));
if (temp == NULL) {
printf("Memory reallocation failed.\n");
free(arr);
return 1;
}
arr = temp;
printf("Enter %d more integers:\n", M);
for (int i = N; i < newSize; i++) {
scanf("%d", &arr[i]);
}
free(arr);
return 0;
}
Problem 14 : Write a C program to dynamically allocate memory for two user-input strings,
concatenate them into a new dynamically allocated string, print the result and free all
allocated memory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str1, *str2, *result;
int len1, len2;
strcpy(result, str1);
strcat(result, str2);
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
int empCode;
float salary;
};
int main() {
struct Employee emp;
printf("Enter employee name: ");
fgets(emp.name, sizeof(emp.name), stdin);
emp.name[strcspn(emp.name, "\n")] = '\0';
printf("Enter employee code: ");
scanf("%d", &emp.empCode);
printf("Enter employee salary: ");
scanf("%f", &emp.salary);
displayEmployee(emp);
return 0;
}
Problem 16 : Develop a C program to read the attributes of an item from the user such as
ItemCode, ItemName, Quantity and Rate. Implement a C program using a structure to find
the total cost of the inventory of storing N items in the stock.
#include <stdio.h>
#include <string.h>
struct Item {
int itemCode;
char itemName[50];
int quantity;
float rate;
};
int main() {
int n, i;
float totalCost = 0;
printf("Quantity: ");
if (scanf("%d", &items[i].quantity) != 1 || items[i].quantity < 0) {
printf("Invalid input.\n");
return 1;
}
printf("Rate: ");
if (scanf("%f", &items[i].rate) != 1 || items[i].rate < 0) {
printf("Invalid input.\n");
return 1;
}
}
#include <stdio.h>
#include <string.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int rollNumber;
struct Date dob;
struct Date doa;
};
int main() {
struct Student s;
return 0;
}
Problem 18 : Develop a C program to read the following details of N employees into
appropriate data storage. EmpNo, Name and Salary. Display all the details of the employees
in the descending order of their salary to the console.
#include <stdio.h>
#include <string.h>
struct Employee {
int empNo;
char name[NAME_LENGTH];
float salary;
};
int main() {
struct Employee emp[MAX_EMPLOYEES];
int n;
printf("Enter the number of employees: ");
if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_EMPLOYEES) {
printf("Invalid number of employees.\n");
return 1;
}
getchar();
getchar();
printf("Name: ");
if (fgets(emp[i].name, sizeof(emp[i].name), stdin) == NULL) {
printf("Error reading name.\n");
return 1;
}
emp[i].name[strcspn(emp[i].name, "\n")] = '\0';
printf("Salary: ");
if (scanf("%f", &emp[i].salary) != 1 || emp[i].salary < 0) {
printf("Invalid input for salary.\n");
return 1;
}
getchar();
}
sortEmployeesBySalary(emp, n);
return 0;
}
Problem 19 : Write a C program to count the number of words, characters, and lines in a text
file.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
FILE *file;
char filename[100];
char ch;
int characters = 0, words = 0, lines = 0;
int inWord = 0;
if (ch == '\n') {
lines++;
}
if (isspace(ch)) {
inWord = 0;
} else if (!inWord) {
inWord = 1;
words++;
}
}
fclose(file);
return 0;
}
Problem 20 : Design a C program to add to an existing file present in the current drive by
reading the contents from another file. Read the names of both the files from the user.
Display the new file to the console by converting all the alphabets to lowercase.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char sourceFileName[100], targetFileName[100];
FILE *sourceFile, *targetFile;
char ch;
fclose(sourceFile);
fclose(targetFile);
fclose(targetFile);
return 0;
}