LAST C FILE (2) Ayush
LAST C FILE (2) Ayush
Experiment No. 23
Aim: Write a program to find the factorial of a number by using the concept of recursion.
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.
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.
void main()
{
int i, j, l, l1, l2;
l1 = strlen(str);
l2 = strlen(sub);
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);
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 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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2310990394
Fundamentals of C Programming (23CS003)
fclose(fptr);
fclose(fptrTemp);
remove(fileName);
rename(tempFileName, fileName);
int main() {
char fileName[MAX_FILE_NAME];
char strToFind[256];
char strToReplace[256];
return 0;
}
Output:
2310990394