Assignment-2 C-Programming.(Program parts)

 Assignment of C


Q1. What is an array ? Write the syntax for single and double dimension array declaration

and write an example of each.

-> 

An array in C is a collection of elements of the same data type, stored in contiguous memory locations. It provides a convenient way to store and manipulate a fixed-size sequential collection of elements.

i. Single dimensional array : A list of items can be given one variable name using only one subscript (or dimension or index) and such a variable is called a single-subscripted variable or a one-dimensional array.

Syntax: storage_class data_type arr_name[size];

* storage_class refers to the storage class of the arrary. It is optional.

* data_type is the data type of array i.e. int, float,...,etc

*array_name is the name of array.

ii. Double dimensional array: A two-dimensional array is an array of one-dimensional arrays. It is mostly used to represent and manipulate tabular data (Matrices).


Q2. Write a program to input 10 numbers an array and display in reverse order.

-> 

//To input 10 numbers into an array and display in reverse order.
#include <stdio.h>

#define SIZE 10

int main() {
int arr[SIZE];
int i;

printf("Enter %d numbers: ", SIZE);
for (i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}

printf("\nNumbers in reverse order:\n");
for (i = SIZE - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}

return 0;
}

Q3. Write a program to input 20 numbers into an array and find out the second smallest and

largest number(LAB)

->

/*To input 20 numbers into an array and find out the second smallest and
largest number */
#include <stdio.h>

#define SIZE 20

int main() {
int arr[SIZE];
int i;

printf("Enter %d numbers: ", SIZE);
for (i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}

// Sort the array in ascending order
for (i = 0; i < SIZE; i++) {
int min = arr[i];
int minIndex = i;
for (int j = i + 1; j < SIZE; j++) {
if (arr[j] < min) {
min = arr[j];
minIndex = j;
}
}
// Swap the minimum value with the current index
int temp = arr[i];
arr[i] = min;
arr[minIndex] = temp;
}

// Find the second smallest and largest numbers
int secondSmallest = arr[1];
int secondLargest = arr[SIZE - 2];

printf("\nSecond smallest number: %d\n", secondSmallest);
printf("Second largest number: %d\n", secondLargest);

return 0;
}

Q4. Write a program to input N numbers into an array and find out the average value of them.

(LAB)

->

/* To input N numbers into an array and find out the average value of them.*/
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int n;
double sum = 0.0;
double average;

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

if (n <= 0 || n > MAX_SIZE) {
printf("Invalid number of elements. Please enter a number between 1 and %d.\n", MAX_SIZE);
return 1;
}

printf("Enter %d numbers: ", n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}

average = sum / n;
printf("Average of entered numbers: %.2lf\n", average);

return 0;
}

Q5. WAP input numbers into a matrix 5x4 and find out the sum of each element. (LAB)

-> 

/*To input numbers into a matrix 5x4 from user and find out the sum of each element.*/
#include <stdio.h>

#define ROWS 5
#define COLS 4

void matrixInput(int mat[][COLS]);
void matrixSum(int mat[][COLS], int sum[][COLS]);

int main() {
int matrix[ROWS][COLS];
int sum[ROWS][COLS];

printf("Enter elements in a 5x4 matrix:\n");
matrixInput(matrix);
matrixSum(matrix, sum);

printf("\nThe sum of each element in the matrix is:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}

void matrixInput(int mat[][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &mat[i][j]);
}
}
}

void matrixSum(int mat[][COLS], int sum[][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
sum[i][j] = mat[i][j] + mat[i][(j + 1) % COLS];
}
}
}

Q6. WAP for matrix addition . (LAB)

-> 

// A program for matrix addition .
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], sumMatrix[MAX_SIZE][MAX_SIZE];
int row, col;
int m1, n1, m2, n2;

printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &m1, &n1);

printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &m2, &n2);

if (m1 != m2 || n1 != n2) {
printf("Matrices must have the same dimensions to be added.\n");
return 1;
}

printf("Enter the elements of the first matrix:\n");
for (row = 0; row < m1; row++) {
for (col = 0; col < n1; col++) {
scanf("%d", &firstMatrix[row][col]);
}
}

printf("Enter the elements of the second matrix:\n");
for (row = 0; row < m2; row++) {
for (col = 0; col < n2; col++) {
scanf("%d", &secondMatrix[row][col]);
}
}

for (row = 0; row < m1; row++) {
for (col = 0; col < n1; col++) {
sumMatrix[row][col] = firstMatrix[row][col] + secondMatrix[row][col];
}
}

printf("Sum of entered matrices:\n");
for (row = 0; row < m1; row++) {
for (col = 0; col < n1; col++) {
printf("%d ", sumMatrix[row][col]);
}
printf("\n");
}

return 0;
}

Q7. WAP for matrix multiplication. (LAB)

-> 

//A program for matrix multiplication.
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], resultMatrix[MAX_SIZE][MAX_SIZE];
int row, col, i, j, k;
int m1, n1, m2, n2;

printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &m1, &n1);

printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &m2, &n2);

if (n1 != m2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}

printf("Enter the elements of the first matrix:\n");
for (row = 0; row < m1; row++) {
for (col = 0; col < n1; col++) {
scanf("%d", &firstMatrix[row][col]);
}
}

printf("Enter the elements of the second matrix:\n");
for (row = 0; row < m2; row++) {
for (col = 0; col < n2; col++) {
scanf("%d", &secondMatrix[row][col]);
}
}

for (row = 0; row < m1; row++) {
for (col = 0; col < n2; col++) {
resultMatrix[row][col] = 0;
for (i = 0; i < n1; i++) {
resultMatrix[row][col] += firstMatrix[row][i] * secondMatrix[i][col];
}
}
}

printf("The result of matrix multiplication is:\n");
for (row = 0; row < m1; row++) {
for (col = 0; col < n2; col++) {
printf("%d ", resultMatrix[row][col]);
}
printf("\n");
}

return 0;
}

Q8. WAP for matrix transpose. (LAB)

-> 

//A program for matrix Transpose.
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int matrix[MAX_SIZE][MAX_SIZE], transpose[MAX_SIZE][MAX_SIZE], row, col;
int m, n;

printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);

if (m > MAX_SIZE || n > MAX_SIZE) {
printf("Matrix dimensions are too large. Please enter values less than %d.\n", MAX_SIZE);
return 1;
}

printf("Enter the elements of the matrix:\n");
for (row = 0; row < m; row++) {
for (col = 0; col < n; col++) {
scanf("%d", &matrix[row][col]);
}
}

for (row = 0; row < m; row++) {
for (col = 0; col < n; col++) {
transpose[col][row] = matrix[row][col];
}
}

printf("The transpose of the matrix is:\n");
for (row = 0; row < n; row++) {
for (col = 0; col < m; col++) {
printf("%d ", transpose[row][col]);
}
printf("\n");
}

return 0;
}

Q9. WAP to input N numbers into an array and find the square root of each number and

display. (LAB)

-> 

/*To input N numbers into an array and find the square root of each number and
display.*/
#include <stdio.h>
#include <math.h>

#define MAX_SIZE 100

int main() {
double array[MAX_SIZE];
int n, i;

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

if (n > MAX_SIZE) {
printf("The number of elements exceeds the maximum allowed.\n");
return 1;
}

printf("Enter %d numbers: ", n);
for (i = 0; i < n; ++i) {
scanf("%lf", &array[i]);
}

printf("\nThe square roots of the entered numbers are:\n");
for (i = 0; i < n; ++i) {
array[i] = sqrt(array[i]);
printf("%.2lf ", array[i]);
}

return 0;
}

Q10. WAP program for number sorting in descending order. (LAB)

->

//A program for number sorting in descending order.
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int n;
int i, j, temp;

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

if (n > MAX_SIZE) {
printf("The number of elements exceeds the maximum allowed.\n");
return 1;
}

printf("Enter %d numbers: ", n);
for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}

// Bubble sort algorithm to sort the array in descending order
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - i - 1; ++j) {
if (arr[j] < arr[j + 1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("\nThe numbers in descending order are:\n");
for (i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}

return 0;
}

Q11. WAP for name sorting in descending order. (LAB)

->

//A program for name sorting in descending order.
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

struct Person {
char name[50];
};

int main() {
struct Person arr[MAX_SIZE];
int n;
int i, j;

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

if (n > MAX_SIZE) {
printf("The number of elements exceeds the maximum allowed.\n");
return 1;
}

printf("Enter %d names: ", n);
for (i = 0; i < n; ++i) {
scanf("%s", arr[i].name);
}

// Bubble sort algorithm to sort the array in descending order
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - i - 1; ++j) {
if (strcmp(arr[j].name, arr[j + 1].name) < 0) {
// Swap the elements
struct Person temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("\nThe names in descending order are:\n");
for (i = 0; i < n; ++i) {
printf("%s\n", arr[i].name);
}

return 0;
}

Q13. What is function? Differentiate between user define function and library function. Also

list advantages of function.

-> 

In C programming, a function is a block of organized, reusable code that performs a specific task. Functions can accept input parameters, process them, and return an output value. They help to organize complex programs and make code more readable and maintainable.

User-defined functions (UDFs) are functions created by the programmer to perform specific tasks in a program. They are not part of the standard C library and must be defined by the programmer before being used in the code.

Library functions, on the other hand, are predefined functions in the C standard library. They are designed to perform common operations and can be used directly without the need to define them in the code. Examples of library functions include printf(), scanf(), sqrt(), and many more.

Here are some advantages of using functions in C programming:

  1. Reusability: Functions can be called multiple times in a program, making it easy to reuse the same code without having to rewrite it.
  2. Modularity: Functions help to break down a program into smaller, manageable parts, making it easier to understand, test, and maintain.
  3. Readability: Using functions can make the code more readable and self-explanatory, as function names often describe the purpose of the code within them.
  4. Encapsulation: Functions can hide the details of their implementation, allowing programmers to focus on the overall logic of the program without getting bogged down in the specifics.
  5. Maintainability: If a change needs to be made to a specific piece of functionality, it can be done in one place (the function definition) rather than having to update multiple instances throughout the code.
  6. Error Reduction: Functions can help reduce the likelihood of errors by allowing programmers to test and debug smaller, more focused sections of code.

Q14. Differentiate between call by value and call by reference function with an example.

-> 

Q15. What do mean by storage classes? Explain each of them.

Q16. Write a program using function to find out the quadratic equation. (LAB)

->

//A program using function to find out the roots of quadratic equation.
#include <stdio.h>
#include <math.h>

void solve_quadratic(double a, double b, double c, double *root1, double *root2) {
double discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
*root1 = (-b + sqrt(discriminant)) / (2 * a);
*root2 = (-b - sqrt(discriminant)) / (2 * a);
} else if (discriminant == 0) {
*root1 = *root2 = -b / (2 * a);
} else {
*root1 = -b / (2 * a);
*root2 = -b / (2 * a);
}
}

int main() {
double a, b, c, root1, root2;

printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

solve_quadratic(a, b, c, &root1, &root2);

printf("The roots of the quadratic equation are: %.2lf and %.2lf\n", root1, root2);

return 0;
}

Q17. Write a program to sort the numbers in ascending order by using user define function

with passing array into function. (LAB)

-> 

/*A program to sort the numbers in ascending order by using user define function
with passing array into function.*/
#include <stdio.h>

void sort_array(int arr[], int size) {
int i, j;
for (i = 0; i < size - 1; i++) {
for (j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

int main() {
int arr[] = {33, 2, 8};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Before sorting:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

sort_array(arr, size);

printf("\nAfter sorting:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Q18. What do mean by prototype function? Explain the type of user define function.

Q19. Write a program to find out the matrix addition using user define function. (LAB)

-> 

/*A program to find out the matrix addition using user define function.*/
#include <stdio.h>

void addMatrix(int rows, int columns, int matrix1[rows][columns], int matrix2[rows][columns], int sumMatrix[rows][columns]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}

int main() {
int rows, columns;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &columns);

int matrix1[rows][columns], matrix2[rows][columns], sumMatrix[rows][columns];

printf("\nEnter elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("Enter element a[%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}

printf("\nEnter elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("Enter element b[%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}

addMatrix(rows, columns, matrix1, matrix2, sumMatrix);

printf("\nSum of the two matrices:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d ", sumMatrix[i][j]);
}
printf("\n");
}

return 0;
}

Q20. What is pointer? Explain the application of pointers.

Q22. What do mean by dynamic memory allocation? Explain with an example.

Q23. WAP to access the element of an array using pointer.

->

//A program to access the element of an array using pointer.
#include <stdio.h>

int main() {
int arr[5];
int *ptr;

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

ptr = arr;

printf("The elements of the array are:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}

return 0;
}

Q24. What is pointer to pointer? Write a program to sort few numbers in ascending order

using dynamic memory allocation. (LAB)

-> 

/*A program to sort few numbers in ascending order
using dynamic memory allocation.*/
#include <stdio.h>
#include <stdlib.h>

int main() {
int num, i, j, *ptr, n;

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

// Allocate memory for n integers
ptr = (int*) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed. Exiting...");
return 1;
}

printf("\nEnter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}

// Bubble sort algorithm
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (ptr[j] > ptr[j + 1]) {
num = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = num;
}
}
}

printf("\nSorted elements in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}

free(ptr);

return 0;
}

Q25. Explain the following funcitons: malloc(), calloc(), and free() with an example.

Q26. What is structure and union? Differentiate between structure, union and array.

Q27. Explain the nested structure with an example.

Q28. What do mean by array of structure? WAP to read 20 employees name and address and

sort them in ascending order based on name using structure. (LAB)

-> 

/*A program to read 20 employees name and address and
sort them in ascending order based on name using structure.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_EMPLOYEES 20

typedef struct {
char name[50];
char address[100];
} Employee;

int compare_employees(const void *a, const void *b) {
Employee *emp1 = (Employee *)a;
Employee *emp2 = (Employee *)b;
return strcmp(emp1->name, emp2->name);
}

int main() {
Employee employees[MAX_EMPLOYEES];
int i;

printf("Enter the names and addresses of %d employees:\n", MAX_EMPLOYEES);
for (i = 0; i < MAX_EMPLOYEES; i++) {
printf("Employee #%d:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Address: ");
scanf("%s", employees[i].address);
}

// Sort the employees array by name
qsort(employees, MAX_EMPLOYEES, sizeof(Employee), compare_employees);

// Print the sorted employees
printf("\nSorted employees by name:\n");
for (i = 0; i < MAX_EMPLOYEES; i++) {
printf("Employee #%d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Address: %s\n", employees[i].address);
}

return 0;
}

Q29. WAP to define the structure of employee having data members name, age, address and

salary. Take the data of N employees and display using dynamically allocated memory

and also display average salary. (LAB)

->

/*A Program to define the structure of employee having data members name, age, address and
salary. Take the data of N employees and display using dynamically allocated memory
and also display average salary.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 5

typedef struct {
char name[30];
int age;
char address[100];
float salary;
} Employee;

int main() {
Employee *employees;
int i;
float sumSalary = 0;

// Allocate memory for N employees
employees = (Employee *) malloc(N * sizeof(Employee));

if (employees == NULL) {
printf("Error allocating memory for employees.\n");
return 1;
}

// Take data of N employees
for (i = 0; i < N; i++) {
printf("Enter employee %d's name: ", i + 1);
scanf("%s", employees[i].name);

printf("Enter employee %d's age: ", i + 1);
scanf("%d", &employees[i].age);

printf("Enter employee %d's address: ", i + 1);
scanf("%s", employees[i].address);

printf("Enter employee %d's salary: ", i + 1);
scanf("%f", &employees[i].salary);
sumSalary += employees[i].salary;
}

// Display employees using dynamically allocated memory
printf("\nDisplaying Employees:\n");
for (i = 0; i < N; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Address: %s\n", employees[i].address);
printf("Salary: NRP%.2f\n", employees[i].salary);
}

// Calculate and display average salary
float averageSalary = sumSalary / N;
printf("\nAverage Salary: NRP%.2f\n", averageSalary);

// Free the allocated memory
free(employees);

return 0;
}

Q30. What is file? Explain the file open modes.

Q31. What do mean by sequential file and random file handling? Explain with an example.

Q32. Write a program to read N students records like as student id, name, address and store

into a file called student.txt. (LAB)

-> 

/*A program to read N students records like as student id, name, address and store
into a file called student.txt.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100

typedef struct {
int id;
char name[30];
char address[100];
} Student;

int main() {
FILE *file;
Student students[MAX_STUDENTS];
int n;

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

if (n > MAX_STUDENTS) {
printf("Maximum number of students allowed is %d.\n", MAX_STUDENTS);
return 1;
}

// Open file for writing
file = fopen("student.txt", "w");

if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}

// Read student records and store them in the file
for (int i = 0; i < n; i++) {
printf("Enter student %d's ID: ", i + 1);
scanf("%d", &students[i].id);

printf("Enter student %d's name: ", i + 1);
scanf("%s", students[i].name);

printf("Enter student %d's address: ", i + 1);
scanf("%s", students[i].address);

fprintf(file, "%d %s %s\n", students[i].id, students[i].name, students[i].address);
}

// Close the file
fclose(file);

printf("Student records saved to student.txt.\n");

return 0;
}

Q33. Write a program to read the contents from above created file student.txt and display.

(LAB)

->

/*A program to read the contents from above created file student.txt
and display.*/
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *file;
char studentId[100];
char studentName[100];
char studentAddress[100];

// Open the student.txt file for reading
file = fopen("student.txt", "r");

if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}

// Read student records and display their information
while (fscanf(file, "%s %s %s", studentId, studentName, studentAddress) != EOF) {
printf("Student ID: %s\n", studentId);
printf("Student Name: %s\n", studentName);
printf("Student Address: %s\n", studentAddress);
printf("\n");
}

// Close the file
fclose(file);

return 0;
}

Q34. Write a program to read N employees records as id, name and salary from the keyboard

and store into a file called “employee.txt” and read the contents from the same file and

display whose salary is between 20000 to 50000. (LAB)

-> 

/*A program to read N employees records as id, name and salary from the keyboard
and store into a file called “employee.txt” and read the contents from the same file and
display whose salary is between 20000 to 50000.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_EMPLOYEES 100

typedef struct {
int id;
char name[30];
float salary;
} Employee;

int main() {
FILE *file;
Employee employees[MAX_EMPLOYEES];
int n, i;

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

if (n > MAX_EMPLOYEES) {
printf("Maximum number of employees allowed is %d.\n", MAX_EMPLOYEES);
return 1;
}

// Read employee records from the keyboard and store them in the "employee.txt" file
for (i = 0; i < n; i++) {
printf("Enter employee %d's ID: ", i + 1);
scanf("%d", &employees[i].id);

printf("Enter employee %d's name: ", i + 1);
scanf("%s", employees[i].name);

printf("Enter employee %d's salary: ", i + 1);
scanf("%f", &employees[i].salary);

// Write employee records to the "employee.txt" file
file = fopen("employee.txt", "a");
if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
fprintf(file, "%d %s %.2f\n", employees[i].id, employees[i].name, employees[i].salary);
fclose(file);
}

// Display employees whose salary is between 20000 and 50000
printf("\nEmployees with salary between 20000 and 50000:\n");
file = fopen("employee.txt", "r");
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
i = 0;
while (fscanf(file, "%d %s %f", &employees[i].id, employees[i].name, &employees[i].salary) != EOF) {
if (employees[i].salary >= 20000 && employees[i].salary <= 50000) {
printf("Employee ID: %d\n", employees[i].id);
printf("Employee Name: %s\n", employees[i].name);
printf("Employee Salary: %.2f\n", employees[i].salary);
}
i++;
}
fclose(file);

return 0;
}

Q35. Write a program to read N employees records as id, name and city from the keyboard and

store into a file called “employee.txt” and read the contents from the same file and

display whose city name is “chitwan” or “Kathmandu”. (LAB)

-> 

/*A program to read N employees records as id, name and city from the keyboard and
store into a file called “employee.txt” and read the contents from the same file and
display whose city name is “chitwan” or “Kathmandu”.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_EMPLOYEES 100

typedef struct {
int id;
char name[30];
char city[30];
} Employee;

int main() {
FILE *file;
Employee employees[MAX_EMPLOYEES];
int n, i;

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

if (n > MAX_EMPLOYEES) {
printf("Maximum number of employees allowed is %d.\n", MAX_EMPLOYEES);
return 1;
}

// Read employee records from the keyboard and store them in the "employee.txt" file
for (i = 0; i < n; i++) {
printf("Enter employee %d's ID: ", i + 1);
scanf("%d", &employees[i].id);

printf("Enter employee %d's name: ", i + 1);
scanf("%s", employees[i].name);

printf("Enter employee %d's city: ", i + 1);
scanf("%s", employees[i].city);

// Write employee records to the "employee.txt" file
file = fopen("employee.txt", "a");
if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
fprintf(file, "%d %s %s\n", employees[i].id, employees[i].name, employees[i].city);
fclose(file);
}

// Display employees whose city name is "chitwan" or "Kathmandu"
printf("\nEmployees with city name 'chitwan' or 'Kathmandu':\n");
file = fopen("employee.txt", "r");
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
i = 0;
while (fscanf(file, "%d %s %s", &employees[i].id, employees[i].name, employees[i].city) != EOF) {
if (strcmp(employees[i].city, "chitwan") == 0 || strcmp(employees[i].city, "Kathmandu") == 0) {
printf("Employee ID: %d\n", employees[i].id);
printf("Employee Name: %s\n", employees[i].name);
printf("Employee City: %s\n", employees[i].city);
}
i++;
}
fclose(file);

return 0;
}

36. Write a c program to copy the contents from “college.txt” file to “staff.txt” file. (LAB)

->

/*A c program to copy the contents from
“college.txt” file to “staff.txt” file.*/
#include <stdio.h>

int main() {
FILE *sourceFile;
FILE *destFile;
int c;

sourceFile = fopen("college.txt", "r");
destFile = fopen("staff.txt", "w");

if (sourceFile == NULL || destFile == NULL) {
printf("Unable to open file.\n");
return 1;
}

while ((c = fgetc(sourceFile)) != EOF) {
fputc(c, destFile);
}

fclose(sourceFile);
fclose(destFile);

return 0;
}


37. Write a Program to calculate and display the volume of a CUBE having its height (h=10cm), width

(w=12cm) and depth (8cm).lab

-> 

/*Write a Program to calculate and display the volume of a CUBE having its height (h=10cm), width
(w=12cm) and depth (8cm).*/
#include <stdio.h>

int main() {
float height, width, depth, volume;

height = 10.0; // cm
width = 12.0; // cm
depth = 8.0; // cm

volume = height * width * depth;

printf("The volume of the cube is: %.2f cm3\n", volume);

return 0;
}

38. Write C program to evaluate each of the following equations. Lab

39. Write a program to find the roots of quadratic equation. Lab

40. Write a program to check number is Armstrong or not. (Hint: A number is Armstrong if the sum of

cubes of individual digits of a number is equal to the number itself).

-> 

/*A program to check number is Armstrong or not. (Hint: A number is Armstrong if the sum of
cubes of individual digits of a number is equal to the number itself).*/
#include <stdio.h>
#include <math.h>

int main() {
int num, sum = 0, temp, digit, n;

printf("Enter a number: ");
scanf("%d", &num);

temp = num;

// Calculate the sum of cubes of individual digits
while (temp != 0) {
digit = temp % 10;
sum += pow(digit, 3);
temp /= 10;
}

// Check if the sum is equal to the number
if (sum == num) {
printf("%d is an Armstrong number.", num);
} else {
printf("%d is not an Armstrong number.", num);
}

return 0;
}

41. Write a program to check whether the entered year is leap year or not (a year is leap if it is divisible

by 4 and divisible by 100 or 400.) lab

-> 

/*A program to check whether the entered year is leap year or not (a year is leap if it is divisible
by 4 and divisible by 100 or 400.)*/
#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");
scanf("%d", &year);

if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

42. Write a program to find GCD (greatest common divisor or HCF) and LCM (least common multiple) of

two numbers. Lab

-> 

/*A program to find GCD (greatest common divisor or HCF) and LCM (least common multiple) of
two numbers.*/
#include <stdio.h>

// Function to find GCD of two numbers using Euclidean Algorithm
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}

// Function to find LCM of two numbers
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;

printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Find GCD and LCM
int g = gcd(num1, num2);
int l = lcm(num1, num2);

printf("GCD of %d and %d is: %d\n", num1, num2, g);
printf("LCM of %d and %d is: %d\n", num1, num2, l);

return 0;
}

43. write a c program to generate the following pattern LAB

44. . Write a program to accept a string and count the number of vowels present in this string.lab

-> 

/*Write a program to accept a string and count the number of vowels present in this string.*/
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int i, count = 0;

printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

for (i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
}
}

printf("Number of vowels in the string: %d\n", count);

return 0;
}

45. Write a program to generate Fibonacci series using recursive function. LAB

-> 

//A program to generate Fibonacci series using recursive function.
#include <stdio.h>

// Recursive function to generate the nth Fibonacci number
int fibonacci(int n) {
// Base case
if (n <= 1)
return n;
else
// Recursive case
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n, i;

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

printf("\nFibonacci Series: ");
for (i = 0; i < n; i++)
printf("%d ", fibonacci(i));

return 0;
}

46. Write a program to find sum of digits of the number using Recursive Function.LAB

-> 

//A program to find sum of digits of the number using Recursive Function.
#include <stdio.h>

// Recursive function to find the sum of digits
int sum_of_digits(int n) {
if (n == 0)
return 0;
return (n % 10 + sum_of_digits(n / 10));
}

int main() {
int num, result;

printf("Enter a number: ");
scanf("%d", &num);

result = sum_of_digits(num);
printf("Sum of digits in %d is %d\n", num, result);

return 0;
}

47. Write a C program to find power of any number using recursion.LAB

-> 

//Write a C program to find power of any number using recursion.
#include <stdio.h>

// Recursive function to calculate the power
int power(int base, int exponent) {
if (exponent == 0)
return 1;
return base * power(base, exponent - 1);
}

int main() {
int base, exponent;

printf("Enter base number: ");
scanf("%d", &base);

printf("Enter exponent: ");
scanf("%d", &exponent);

printf("%d raised to the power of %d is %d\n", base, exponent, power(base, exponent));

return 0;
}

48. Write a program to compare two strings using pointers. Lab

-> 

//Write a C program to compare two strings using pointers.
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];
int i = 0, result;

printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);

printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);

while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
result = 1;
break;
}
i++;
}

if (result == 1) {
printf("The strings are not equal.\n");
} else {
printf("The strings are equal.\n");
}

return 0;
}

49. Write a program to input a line of text and store into a file called “line.txt” and read the text from

same file and count total number of vowel. lab

-> 

/*A program to input a line of text and store into a file called “line.txt” and read the text from
same file and count total number of vowel.*/
#include <stdio.h>
#include <string.h>

int main() {
FILE *filePtr;
char line[100];
int vowels = 0, i = 0, ch;

// Input a line of text
printf("Enter a line of text: ");
fgets(line, sizeof(line), stdin);

// Open or create the file "line.txt" in write mode
filePtr = fopen("line.txt", "w");

// Write the text to the file
fprintf(filePtr, "%s", line);
fclose(filePtr);

// Open the file "line.txt" in read mode
filePtr = fopen("line.txt", "r");

// Read each character from the file and count the vowels
while ((ch = fgetc(filePtr)) != EOF) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowels++;
}
}

// Print the total number of vowels
printf("Total vowels in the text: %d\n", vowels);

return 0;
}

50. write a program to copy the content from a file “source.txt” into “dest.txt”. lab

-> Repetative que.

51. write a down file open modes.


 ******

No need to thank but you can definetly help UJJU_Rizz-ALL by listening this vid full:

link: TAP_ME / CLICK_ON_ME


Name: Ujjwal Rijal  

Address: Earth

Instagram: www.instagram.com/rijalujjwal87

Twitter: www.x.com/ujjwalreezal

Facebook: www.facebook.com/UjjuGod

Youtube : www.youtube.com/@fyaste

 


Comments

Popular posts from this blog

LAB WORK OF PHYSICS (CSIT 1st Sem)

LAB WORK OF DIGITAL LOGICS: (CSIT 1st SEM)

CONQUER IIT(1st SEM) EXAM targeted !!