0% found this document useful (0 votes)
29 views22 pages

LAST C FILE (2) Ayush

Uploaded by

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

LAST C FILE (2) Ayush

Uploaded by

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

Fundamentals of C Programming (23CS003)

Experiment No. 23

Aim: Write a program to find the factorial of a number by using the concept of recursion.

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int number;
printf("Enter a Positive Integer = ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int result = factorial(number);
printf("Factorial of %d is %d\n", number, result);
}
return 0;
}

Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 24

Aim: Write a menu driven C program to show the use of in-built string functions like strlen, strcat, strcpy,
strcmp, strrev etc.

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>
#include <string.h>

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

do {
printf("\nMenu:\n");
printf("1. Find length of a string\n");
printf("2. Concatenate two strings\n");
printf("3. Copy a string\n");
printf("4. Compare two strings\n");
printf("5. Reverse a string\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a string: ");
scanf("%s", str1);
printf("Length of the string: %lu\n", strlen(str1));
break;
case 2:
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
2310990394
Fundamentals of C Programming (23CS003)
scanf("%s", str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
break;
case 3:
printf("Enter a string: ");
scanf("%s", str1);
strcpy(temp, str1);
printf("Copied string: %s\n", temp);
break;
case 4:
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
break;
case 5:
printf("Enter a string: ");
scanf("%s", str1);
strcpy(temp, str1);
strrev(temp);
printf("Reversed string: %s\n", temp);
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 6);

return 0;
}

2310990394
Fundamentals of C Programming (23CS003)

Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 25

Aim: Write a Program in C to display the total number of appearances of a substring provided as input by
the user in a given string.

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>
#include <string.h>

char str[100], sub[100];


int count = 0, count1 = 0;

void main()
{
int i, j, l, l1, l2;

printf("\nEnter a string : ");


scanf("%[^\n]s", str);

l1 = strlen(str);

printf("\nEnter a substring : ");


scanf(" %[^\n]s", sub);

l2 = strlen(sub);

for (i = 0; i < l1;)


{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;

2310990394
Fundamentals of C Programming (23CS003)
}
if (count == l2)
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times in %s", sub, count1, str);
}

Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 26

Aim: Write a program to display the sum of the digits of a number by using the concept of recursion.
Program Used: GDB Compiler
Solution:
Implementation:

#include <stdio.h>
int sumOfDigits(int num) {
if (num < 10)
return num;
else
return num % 10 + sumOfDigits(num / 10);
}

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int sum = sumOfDigits(number);
printf("Sum of digits of %d = %d\n", number, sum);

return 0;
}

Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 27

Aim: Write a C program to add two distances in inch & feet using the concept of structures.
Program Used: GDB Compiler
Solution:
Implementation:

#include <stdio.h>
struct Distance {
int feet;
int inches;
};
struct Distance addDistances(struct Distance d1, struct Distance d2) {
struct Distance result;
result.feet = d1.feet + d2.feet;
result.inches = d1.inches + d2.inches;
if (result.inches >= 12) {
result.feet += result.inches / 12;
result.inches %= 12;
}
return result;
}
int main() {
struct Distance distance1, distance2, sum;
printf("Enter first distance =\n");
printf("Feet = ");
scanf("%d", &distance1.feet);

2310990394
Fundamentals of C Programming (23CS003)
printf("Inches = ");
scanf("%d", &distance1.inches);
printf("Enter second distance =\n");
printf("Feet = ");
scanf("%d", &distance2.feet);
printf("Inches = ");
scanf("%d", &distance2.inches);
sum = addDistances(distance1, distance2);
printf("Sum of distances: %d feet %d inches\n", sum.feet,
sum.inches); return 0;
}

Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 28

Aim: Write a C program to add two complex numbers using the concept of structures in C.
Program Used: GDB Compiler
Solution:
Implementation:

#include <stdio.h>
struct Complex {
float real;
float imag;
};
struct Complex addComplex(struct Complex num1, struct Complex num2) {
struct Complex result;
result.real = num1.real + num2.real;
result.imag = num1.imag + num2.imag;
return result;
}

int main() {
struct Complex num1, num2, sum;
printf("Enter real and imaginary parts of first complex number:\
n"); printf("Real part: ");
scanf("%f", &num1.real);
printf("Imaginary part: ");
scanf("%f", &num1.imag);

printf("Enter real and imaginary parts of second complex number:\


n"); printf("Real part: ");
scanf("%f", &num2.real);
printf("Imaginary part: ");
scanf("%f", &num2.imag);
sum = addComplex(num1, num2);
printf("Sum = %.2f + %.2fi\n", sum.real, sum.imag);

return 0;
}

2310990394
Fundamentals of C Programming (23CS003)
Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 29
Aim: Write a program in C to store the information of five employees using both concepts i.e. array of
structure and array within structure.
Program Used: GDB Compiler
Solution: a) Array of Structure
Implementation:

#include <stdio.h>

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

int main() {
struct Employee employees[5];
printf("Enter information for 5 employees:\n");
for (int i = 0; i < 5; ++i) {
printf("Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].name);

printf("ID: ");
scanf("%d", &employees[i].id);

printf("Salary: ");

2310990394
Fundamentals of C Programming (23CS003)
scanf("%f", &employees[i].salary);
}
printf("\nInformation of all employees:\n");
for (int i = 0; i < 5; ++i) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("ID: %d\n", employees[i].id);
printf("Salary: %.2f\n", employees[i].salary);
printf("\n");
}

return 0;
}

2310990394
Fundamentals of C Programming (23CS003)
Output:

2310990394
Fundamentals of C Programming (23CS003)
Solution: b) Array withing Structure
Implementation:
#include <stdio.h>
struct Employee {
int empId;
char name[50];
float salary;
};

int main() {
struct Employee employees[5];
for (int i = 0; i < 5; i++) {
printf("Enter details for employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].empId);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}
printf("\nEmployee Information:\n");
for (int i = 0; i < 5; i++) {
printf("Employee %d\n", i + 1);
printf("ID: %d\n", employees[i].empId);
printf("Name: %s\n", employees[i].name);
printf("Salary: %.2f\n", employees[i].salary);

2310990394
Fundamentals of C Programming (23CS003)
printf("\n");
}
return 0;
}
Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 30

Aim: Write a Program in C to store and retrieve the information about students of a university by using the
concept of file handling.
Program Used: GDB Compiler
Solution:
Implementation:

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

struct Student {
char name[50];
int roll_number;
int age;
double total_marks;
};

void storeStudentInfo(struct Student student) {


FILE *fptr;
fptr = fopen("student_info.txt", "a");
if (fptr == NULL) {
printf("File does not exist\n");
return;
}
fprintf(fptr, "Name = %s\n", student.name);
fprintf(fptr, "Roll Number = %d\n",
student.roll_number); fprintf(fptr, "Age = %d\n",
student.age);
fprintf(fptr, "Total Marks = %.2f\n\n",
student.total_marks); fclose(fptr);
}

void retrieveStudentInfo() {
FILE *fptr;
char str[100];
fptr = fopen("student_info.txt", "r");
if (fptr == NULL) {
2310990394
Fundamentals of C Programming (23CS003)
printf("File does not exist\n");
return;
}
while (fgets(str, sizeof(str), fptr)!= NULL)
{ printf("%s", str);
}
fclose(fptr);
}

int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student student[n];
for (int i = 0; i < n; i++) {
printf("Enter the name of student %d: ", i + 1);
scanf("%s", student[i].name);
printf("Enter the roll number of student %d: ", i + 1);
scanf("%d", &student[i].roll_number);
printf("Enter the age of student %d: ", i + 1);
scanf("%d", &student[i].age);
printf("Enter the total marks of student %d: ", i + 1);
scanf("%lf", &student[i].total_marks);
storeStudentInfo(student[i]);
}
printf("Student information stored successfully!\n");
printf("Retrieving student information...\n");
retrieveStudentInfo();
return 0;
}

2310990394
Fundamentals of C Programming (23CS003)
Output:

2310990394
Fundamentals of C Programming (23CS003)

Experiment No. 31

Aim: Write a Program in C to find and replace a specific string in a file and also display the total number of
appearances of that string.

Program Used: GDB Compiler


Solution:
Implementation:

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

#define MAX_LINE_LENGTH 1024


#define MAX_FILE_NAME 256

void findAndReplace(char *fileName, char *strToFind, char *strToReplace)


{ FILE *fptr, *fptrTemp;
char line[MAX_LINE_LENGTH];
char tempFileName[] = "temp.txt";
int count = 0;

fptr = fopen(fileName, "r");


fptrTemp = fopen(tempFileName, "w");
if (fptr == NULL || fptrTemp == NULL)
{ printf("File does not exist\n");
return;
}

while (fgets(line, sizeof(line), fptr) != NULL) {


char *pos = strstr(line, strToFind);
while (pos != NULL) {
count++;
pos = strstr(pos + 1, strToFind);
}
fprintf(fptrTemp, "%s", strstr(line, strToFind) == NULL ? line : strtok(strcat(strtok(line,
strToFind), strToReplace), strToFind));
}

2310990394
Fundamentals of C Programming (23CS003)

fclose(fptr);
fclose(fptrTemp);

remove(fileName);
rename(tempFileName, fileName);

printf("Total appearances of '%s': %d\n", strToFind, count);


}

int main() {
char fileName[MAX_FILE_NAME];
char strToFind[256];
char strToReplace[256];

printf("Enter the file name: ");


scanf("%s", fileName);
printf("Enter the string to find: ");
scanf("%s", strToFind);
printf("Enter the string to replace: ");
scanf("%s", strToReplace);

findAndReplace(fileName, strToFind, strToReplace);

return 0;
}

Output:

2310990394

You might also like