Practical - File - C - Programing (1) .Docx FAIZAN 03
Practical - File - C - Programing (1) .Docx FAIZAN 03
INDEX
1 Print the elements of an array in reverse order
#include <stdio.h>
#include <conio.h>
int main() {
int n;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array in reverse order: ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
getch();
return 0;
}
2. Print the largest element present in an array
#include <stdio.h>
#include <conio.h>
int main() {
int n, largest;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("Largest element: %d", largest);
getch();
return 0;
}
3. Sort the elements of an array in ascending order
#include <stdio.h>
#include <conio.h>
int main() {
int n, temp;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
getch();
return 0;
}
4. Calculate the addition of 2 matrices
#include <stdio.h>
#include <conio.h>
int main() {
int rows, cols;
clrscr();
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int mat1[rows][cols], mat2[rows][cols], sum[rows][cols];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("Resultant matrix (Addition):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
getch();
return 0;
}
5. Calculate the multiplication of 2 matrices
#include <stdio.h>
#include <conio.h>
int main() {
int rows1, cols1, rows2, cols2;
clrscr();
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication not possible.");
getch();
return 0;
}
int mat1[rows1][cols1], mat2[rows2][cols2], prod[rows1][cols2];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
prod[i][j] = 0;
for (int k = 0; k < cols1; k++) {
prod[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Resultant matrix (Multiplication):\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", prod[i][j]);
}
printf("\n");
}
getch();
return 0;
}
6. Show the basic declaration of a pointer
#include <stdio.h>
#include <conio.h>
int main() {
int x, *ptr;
clrscr();
printf("Enter a number: ");
scanf("%d", &x);
ptr = &x;
printf("Pointer points to value: %d", *ptr);
getch();
return 0;
}
7. Demonstrate the use of &(address of) and (value at address) operators
#include <stdio.h>
#include <conio.h>
int main() {
int x;
clrscr();
printf("Enter a number: ");
scanf("%d", &x);
printf("Address of x: %p\n", &x);
printf("Value at address of x: %d", *(&x));
getch();
return 0;
}
8. Store n elements in an array and print the elements using a pointer
#include <stdio.h>
#include <conio.h>
int main() {
int n;
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], *ptr;
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
ptr = arr;
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
getch();
return 0;
}
9. Find the factorial of a given number using pointers
#include <stdio.h>
#include <conio.h>
int factorial(int *n) {
int fact = 1;
for (int i = 1; i <= *n; i++) {
fact *= i;
}
return fact;
}
int main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is: %d", num, factorial(&num));
getch();
return 0;
}
10. Demonstrate various String Functions
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
clrscr();
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
printf("String length of '%s': %lu\n", str1, strlen(str1));
printf("String comparison result: %d\n", strcmp(str1, str2));
printf("Concatenated string: %s", strcat(str1, str2));
getch();
return 0;
}
11. Count the total number of alphabets, digits, and special characters in a string
#include <stdio.h>
#include <conio.h>
int main() {
char str[100];
int alphabets = 0, digits = 0, special = 0;
clrscr();
printf("Enter a string: ");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
alphabets++;
} else if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else {
special++;
}
}
printf("Alphabets: %d, Digits: %d, Special Characters: %d", alphabets, digits, special);
getch();
return 0;
}
12. Find the number of times the word 'the' appears in a string
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str[200], word[10] = "the";
int count = 0;
clrscr();
printf("Enter a string: ");
gets(str);
char *token = strtok(str, " ");
while (token != NULL) {
if (strcmp(token, word) == 0) {
count++;
}
token = strtok(NULL, " ");
}
printf("The word 'the' appears %d times.", count);
getch();
return 0;
}
13. Compare two strings without using string library functions
#include <stdio.h>
#include <conio.h>
int compareStrings(char str1[], char str2[]) {
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++) {
if (str1[i] != str2[i]) {
return 0;
}
}
return 1;
}
int main() {
char str1[100], str2[100];
clrscr();
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (compareStrings(str1, str2)) {
printf("Strings are equal.");
} else {
printf("Strings are not equal.");
}
getch();
return 0;
}
14. Display the values of Structure members
#include <stdio.h>
#include <conio.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person;
clrscr();
printf("Enter name: ");
scanf("%s", person.name);
printf("Enter age: ");
scanf("%d", &person.age);
printf("Enter height: ");
scanf("%f", &person.height);
printf("Name: %s\nAge: %d\nHeight: %.2f", person.name, person.age, person.height);
getch();
return 0;
}
15. Store student information and display it
#include <stdio.h>
#include <conio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student student;
clrscr();
printf("Enter student ID: ");
scanf("%d", &student.id);
printf("Enter student name: ");
scanf("%s", student.name);
printf("Enter student marks: ");
scanf("%f", &student.marks);
printf("Student ID: %d\nName: %s\nMarks: %.2f", student.id, student.name, student.marks);
getch();
return 0;
}
16. Print the date, month, and year of a student’s Date of Birth using nested Structures
#include <stdio.h>
#include <conio.h>
struct Date {
int day, month, year;
};
struct Student {
char name[50];
struct Date dob;
};
int main() {
struct Student student;
clrscr();
printf("Enter student name: ");
scanf("%s", student.name);
printf("Enter date of birth (day month year): ");
scanf("%d %d %d", &student.dob.day, &student.dob.month, &student.dob.year);
printf("Student Name: %s\nDate of Birth: %02d-%02d-%04d", student.name,
student.dob.day, student.dob.month, student.dob.year);
getch();
return 0;
}
int main() {
struct ExampleStruct s = {10, 'A', 3.14};
union ExampleUnion u;
clrscr();
u.num = 10;
u.ch = 'A';
u.dec = 3.14;
printf("Structure: num = %d, ch = %c, dec = %.2f\n", s.num, s.ch, s.dec);
printf("Union: num = %d, ch = %c, dec = %.2f\n", u.num, u.ch, u.dec);
getch();
return 0;
}
18. Open and Close a file
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
char content[101];
clrscr();
file = fopen("file.txt", "r");
if (file != NULL) {
fgets(content, 100, file);
printf("Content read from file: %s", content);
fclose(file);
} else {
printf("Failed to open the file.");
}
getch();
return 0;
}
20. Write 100 characters to a file
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
char content[101];
clrscr();
printf("Enter content to write to the file (max 100 characters): ");
fgets(content, 100, stdin);
file = fopen("file.txt", "w");
if (file != NULL) {
fputs(content, file);
printf("Content written to the file successfully.");
fclose(file);
} else {
printf("Failed to open the file.");
}
getch();
return 0;
}
21. Append 100 characters to a file
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
char content[101];
clrscr();
printf("Enter content to append to the file (max 100 characters): ");
fgets(content, 100, stdin);
file = fopen("file.txt", "a");
if (file != NULL) {
fputs(content, file);
printf("Content appended to the file successfully.");
fclose(file);
} else {
printf("Failed to open the file.");
}
getch();
return 0;
}
22. Demonstrate basic string operations
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
char str1[100], str2[100], copiedStr1[100], copiedStr2[100];
int length, comparison;
clrscr();
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
strcpy(copiedStr1, str1);
strcpy(copiedStr2, str2);
strcap(copiedStr1);
strcap(copiedStr2);
length = strlen(copiedStr1);
comparison = strcmp(copiedStr1, copiedStr2);
if (comparison == 0) {
printf("The strings are equal.\n");
} else if (comparison < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
strcat(copiedStr1, copiedStr2);
printf("Concatenated string: %s", copiedStr1);
getch();
return 0;
}