0% found this document useful (0 votes)
24 views23 pages

MCS 011

Uploaded by

sumant1352003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views23 pages

MCS 011

Uploaded by

sumant1352003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Q.

#include <stdio.h>

unsigned long long factorial_iterative(int n) {


unsigned long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d (Iterative): %llu\n", num, factorial_iterative(num));
return 0;
}
Q.1
#include <stdio.h>

unsigned long long factorial_recursive(int n) {


if (n == 0 || n == 1) {
return 1;
}
return n * factorial_recursive(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d (Recursive): %llu\n", num, factorial_recursive(num));
return 0;
}

Q.2
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 2
Element 2: 4
Element 3: 6
Element 4: 8
Element 5: 10

The sum of all elements in the array is: 30


#include <stdio.h>

int main() {
int n, i, sum = 0;

// Prompt user for the number of elements in the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];
// Input elements into the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Calculate the sum of all elements in the array
for (i = 0; i < n; i++) {
sum += arr[i];
}
// Output the result
printf("The sum of all elements in the array is: %d\n", sum);

return 0;
}
Q.3

#include <stdio.h>
#include <stdlib.h>

#define MAX 10 // Maximum size of the matrix

// Function to calculate the determinant of a matrix


float determinant(float matrix[MAX][MAX], int n) {
float det = 0;
float submatrix[MAX][MAX];
if (n == 2) {
return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
}
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x) {
continue;
}
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det += (x % 2 == 0 ? 1 : -1) * matrix[0][x] * determinant(submatrix, n - 1);
}
return det;
}

// Function to calculate the cofactor matrix


void cofactor(float matrix[MAX][MAX], float cof[MAX][MAX], int n) {
float submatrix[MAX][MAX];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int subi = 0;
for (int x = 0; x < n; x++) {
if (x == i) {
continue;
}
int subj = 0;
for (int y = 0; y < n; y++) {
if (y == j) {
continue;
}
submatrix[subi][subj] = matrix[x][y];
subj++;
}
subi++;
}
cof[j][i] = ((i + j) % 2 == 0 ? 1 : -1) * determinant(submatrix, n - 1);
}
}
}

// Function to find the inverse of a matrix


int inverse(float matrix[MAX][MAX], float inverse[MAX][MAX], int n) {
float det = determinant(matrix, n);
if (det == 0) {
return 0; // Not invertible
}
float cof[MAX][MAX];
cofactor(matrix, cof, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
inverse[i][j] = cof[i][j] / det;
}
}
return 1;
}

// Function to multiply two matrices


void multiply(float A[MAX][MAX], float B[MAX][MAX], float C[MAX][MAX], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

int main() {
int n;
float A[MAX][MAX], B[MAX][MAX], B_inv[MAX][MAX], C[MAX][MAX];
// Input matrix size
printf("Enter the size of the matrices (N): ");
scanf("%d", &n);

if (n > MAX) {
printf("Matrix size exceeds the maximum allowed (%d).\n", MAX);
return 1;
}
// Input matrix A
printf("Enter elements of matrix A (%d x %d):\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%f", &A[i][j]);
}
}
// Input matrix B
printf("Enter elements of matrix B (%d x %d):\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%f", &B[i][j]);
}
}
// Calculate the inverse of B
if (!inverse(B, B_inv, n)) {
printf("Matrix B is not invertible. Division is not possible.\n");
return 1;
}
// Multiply A with the inverse of B
multiply(A, B_inv, C, n);

// Output result
printf("Resultant matrix C (A / B):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%0.2f ", C[i][j]);
}
printf("\n");
}

return 0;
Enter the size of the matrices (N): 2
Enter elements of matrix A (2 x 2):
12
34
Enter elements of matrix B (2 x 2):
20
02

Resultant matrix C (A / B):


0.50 1.00
1.50 2.00
Q.4

#include <stdio.h>

// Function to find string length


int string_length(char str[]) {
int i = 0;
while (str[i] != '\0') {
i++;
}
return i;
}

// Function to concatenate two strings


void string_concatenate(char str1[], char str2[], char result[]) {
int i = 0, j = 0;

// Copy first string into result


while (str1[i] != '\0') {
result[i] = str1[i];
i++;
}

// Append second string


while (str2[j] != '\0') {
result[i] = str2[j];
i++;
j++;
}
result[i] = '\0';
}

// Function to copy one string into another


void string_copy(char source[], char destination[]) {
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
}

// Function to compare two strings


int string_compare(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return str1[i] - str2[i];
}
i++;
}
return str1[i] - str2[i];
}

int main() {
char str1[100], str2[100], result[200];
int choice;

// Menu
printf("Choose an option:\n");
printf("1. Find string length\n");
printf("2. Concatenate two strings\n");
printf("3. Copy one string to another\n");
printf("4. Compare two strings\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear the input buffer

switch (choice) {
case 1: // String length
printf("Enter a string: ");
gets(str1);
printf("The length of the string is: %d\n", string_length(str1));
break;

case 2: // String concatenation


printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
string_concatenate(str1, str2, result);
printf("The concatenated string is: %s\n", result);
break;

case 3: // String copy


printf("Enter the source string: ");
gets(str1);
string_copy(str1, result);
printf("The copied string is: %s\n", result);
break;

case 4: // String comparison


printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
int cmp = string_compare(str1, str2);
if (cmp == 0) {
printf("The strings are equal.\n");
} else if (cmp > 0) {
printf("The first string is greater.\n");
} else {
printf("The second string is greater.\n");
}
break;

default:
printf("Invalid choice. Please try again.\n");
}

return 0;
}
Q .5

#include <stdio.h>

// Function to find the maximum value in an array using pointers


int find_max(int *arr, int size) {
int max = *arr; // Initialize max with the first element
for (int i = 1; i < size; i++) {
if (*(arr + i) > max) {
max = *(arr + i);
}
}
return max;
}

// Function to find the minimum value in an array using pointers


int find_min(int *arr, int size) {
int min = *arr; // Initialize min with the first element
for (int i = 1; i < size; i++) {
if (*(arr + i) < min) {
min = *(arr + i);
}
}
return min;
}
// Function to reverse an array using pointers
void reverse_array(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
while (start < end) {
// Swap values at start and end
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

// Function to display the array


void display_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
}

int main() {
int arr[100], n;

// Input array size


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Display original array


printf("\nOriginal array:\n");
display_array(arr, n);

// Find and display maximum and minimum values


int max = find_max(arr, n);
int min = find_min(arr, n);
printf("Maximum value: %d\n", max);
printf("Minimum value: %d\n", min);

// Reverse the array and display it


reverse_array(arr, n);
printf("Reversed array:\n");
display_array(arr, n);

return 0;
}
Q.6

#include <stdio.h>
#include <string.h>

#define MAX 100

// Define a structure for student information


struct Student {
char name[50];
int roll_number;
float marks;
};

// Function to input student records


void input_students(struct Student students[], int n) {
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Name: ");
getchar(); // Clear input buffer
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0'; // Remove
newline
printf("Roll number: ");
scanf("%d", &students[i].roll_number);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
}

// Function to display student records


void display_students(struct Student students[], int n) {
printf("\nStudent Records:\n");
printf("--------------------------------------------------\n");
printf("Name\t\tRoll Number\tMarks\n");
printf("--------------------------------------------------\n");
for (int i = 0; i < n; i++) {
printf("%-15s%-15d%-10.2f\n", students[i].name, students[i].roll_number,
students[i].marks);
}
printf("--------------------------------------------------\n");
}

// Function to sort students by marks


void sort_students_by_marks(struct Student students[], int n) {
struct Student temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].marks < students[j + 1].marks) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}

int main() {
struct Student students[MAX];
int n;

printf("Enter the number of students: ");


scanf("%d", &n);

// Input student details


input_students(students, n);

// Display student details


display_students(students, n);

// Sort and display sorted records


sort_students_by_marks(students, n);
printf("\nAfter sorting by marks (descending):\n");
display_students(students, n);

return 0;
}
Q.7

#include <stdio.h>
#include <stdlib.h>

void process_file(const char *input_file, const char *output_file) {


FILE *in_file, *out_file;
int number, sum = 0, count = 0;
float average;

// Open the input file for reading


in_file = fopen(input_file, "r");
if (in_file == NULL) {
printf("Error: Could not open file %s for reading.\n", input_file);
exit(1);
}

// Read numbers from the file and calculate sum and count
while (fscanf(in_file, "%d", &number) != EOF) {
sum += number;
count++;
}
fclose(in_file); // Close the input file

// Calculate the average


if (count > 0) {
average = (float)sum / count;
} else {
average = 0.0;
}

// Open the output file for writing


out_file = fopen(output_file, "w");
if (out_file == NULL) {
printf("Error: Could not open file %s for writing.\n", output_file);
exit(1);
}

// Write the results to the output file


fprintf(out_file, "Sum: %d\n", sum);
fprintf(out_file, "Average: %.2f\n", average);
fclose(out_file); // Close the output file

printf("Data processed successfully. Results written to %s.\n", output_file);


}
int main() {
const char *input_file = "input.txt";
const char *output_file = "output.txt";
process_file(input_file, output_file);

return 0;
}
Q.8

#include <stdio.h>
// Define constants using macros
#define PI 3.14159
#define SQUARE(x) ((x) * (x)) // Inline calculation using a macro
// Conditional compilation
#define DEBUG_MODE // Uncomment to enable debug mode
int main() {
float radius, area;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

// Calculate the area of the circle


area = PI * SQUARE(radius);

printf("The area of the circle is: %.2f\n", area);


// Debug mode: Print detailed calculations
#ifdef DEBUG_MODE
printf("[DEBUG] Radius: %.2f\n", radius);
printf("[DEBUG] Area Calculation: PI (%.5f) * SQUARE(%.2f)\n", PI, radius);
#endif

return 0;
}

You might also like