0% found this document useful (0 votes)
6 views47 pages

c Programming Priyanshu (1)

Uploaded by

senprince.2005
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)
6 views47 pages

c Programming Priyanshu (1)

Uploaded by

senprince.2005
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/ 47

GEETA UNIVERSITY, PANIPAT

GEETA UNIVERSITY
PANIPAT

BTech 1st Semester


School of Computer Science
(2024-2025)

Problem Solving with C Lab

(CS3143)

Lab Practical file

Roll no = 2401301024

Submitted To: - Submitted by: -

Mr. Vishal Yadav Priyanshu


GEETA UNIVERSITY, PANIPAT

LAB PRACTICAL FILE

DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

PAGE
S.no. LIST OF EXPERIMENT DATE SIGN.
NO.
Write a C Program to compute an electricity bill based on
1a.
the slab rates.
Write a C program to determine insurance eligibility,
1b.
premium rate, and maximum.
Write a C Program to find a student's grade using a Switch
1c.
case.
Write a C program to generate the first n terms of the
2a.
Fibonacci sequence
Write a C program to find the sum of individual digits of a
2b.
positive integer.
Write a C program to compute the sum of a geometric
2c.
progression: 1+x+x^2+...+x^n.
2d. Write a C program to print a pyramid pattern of numbers.
Write a C program to find the largest and smallest
3a.
numbers in a list of integers.
Write a C program to perform addition and multiplication
3b.
of two matrices.
Write a C program to insert a sub-string into a main string
4a.
at a given position.
Write a C program to count the lines, words, and
4b.
characters in a given text.
Write a C program to generate all prime numbers between
5a.
1 and n.
Write a C program using recursion to find the factorial and
5b.
GCD of numbers.
Write a C program to print the elements of an array in
6a.
reverse order using pointer.
Write a C program to count vowels and consonants in a
6b.
string using pointers.
6c. Write a C program to sort n array elements using pointers.
Write a C program for operations on complex numbers:
7a.
read, write, addition, Multiplication.
Write a C program using structures to manage employee
7b.
details and perform operations.
Write a C program to reverse the first n characters of a text
8a.
file.
8b. Write a C program to merge two files into a new file.
Write a C program to develop a phone book app for managing
9.
contact details.
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C Program to compute an electricity bill based on EXPER. No. 1
the slab rates.
PAGE No. 3
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-1
Objective:

CODE:

#include <stdio.h>
int main()
{
float old_reading;
printf("Enter the old reading of the meter: ");
scanf("%f", &old_reading);
float current_reading;
printf("Enter the current reading of the meter: ");
scanf("%f", &current_reading);

float unit_comsumed = current_reading - old_reading;


float amount = 0;
if (unit_comsumed > 300)
{
amount = amount + (unit_comsumed - 300) * 8.0;
unit_comsumed = 300;
}
if (unit_comsumed > 200)
{
amount = amount + (unit_comsumed - 200) * 6.3;
unit_comsumed = 200;
}
if (unit_comsumed > 150)
{
amount = amount + (unit_comsumed - 150) * 5.2;
unit_comsumed = 150;
}
if (unit_comsumed > 100)
{
amount = amount + (unit_comsumed - 100) * 4.6;
unit_comsumed = 100;
}
if (unit_comsumed > 0)
{
amount = amount + unit_comsumed * 4.0;
}
printf("The final bill of your consumption is : %.2f", amount);
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to determine insurance eligibility, EXPER. No. 1
premium rate, and maximum.
PAGE No. 5
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-1

Objective:

CODE:

#include <stdio.h>
#include <string.h>
int main()
{
char health[32];
printf("Tell us about your health i.g.(excellent or poor) :");
scanf("%s", health);
int age;
printf("Enter your age :");
scanf("%d", &age);
char gender[32];
printf("Tell us your gender i.g.(male or female) :");
scanf("%s", gender);
char lives[32];
printf("Tell us where do you live i.g.(city or village) :");
scanf("%s", lives);
if (strcmp(health, "excellent") == 0)
{
if (age > 25 && age < 35)
{
if (strcmp(lives, "city") == 0)
{
if (strcmp(gender, "male") == 0)
{
printf("Your premium rate of insurance is 4 Rs. per 1000 and The amount can not exceed Rs. 2lakh.");
}
else
{
printf("Your premium rate of insurance is 3 Rs. per 1000 and The amount can not exceed Rs. 1lakh.");
}
}
}
}
else if (strcmp(health, "poor") == 0)
{
if (age > 25 && age < 35)
{
if (strcmp(lives, "village") == 0)
{
if (strcmp(gender, "male") == 0)
{
printf("Your premium rate of insurance is 6 Rs. per 1000 and The amount can not exceed Rs. 10,000.");
}
}
}
}
else
{
printf("You are not insured.");
}
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C Program to find a student's grade using a Switch EXPER. No. 1
case.
PAGE No. 7
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-1

Objective:

CODE:

#include <stdio.h>
int main()
{

int score;
printf("Enter your score i.g. (0 to 100):");
scanf("%d", &score);
int cases=0;
if(score>=90){
cases=1;
}
else if(score<90 && score>=80){
cases=2;
}
else if(score<80 && score>=70){
cases=3;
}
else if(score<70 && score>=60){
cases=4;
}
else if(score<60 && score>=50){
cases=5;
}
else if(score<50 && score>=40){
cases=6;
}
else{
cases=7;
}
switch (cases)
{
case 1:
printf("Your grade is : O");
break;
case 2:
printf("Your grade is : A");
break;
case 3:
printf("Your grade is : B");
break;
case 4:
printf("Your grade is : C");
break;
case 5:
printf("Your grade is : D");
break;
case 6:
printf("Your grade is : E");
break;
case 7:
printf("You are Fail.");
break;
default:
printf("error");
break;
}
return 0;
}
OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to generate the first n terms of the EXPER. No. 2
Fibonacci sequence
PAGE No. 10
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-2

Objective:

CODE:

#include<stdio.h>
int main(){
int n, first = 0, second = 1, next;
printf("Enter the number of terms for the Fibonacci series: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i == 0) {
printf("%d ", first);
continue;
}
if (i == 1) {
printf("%d ", second);
continue;
}
next = first + second;
first = second;
second = next;
printf("%d ", next);
}
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to find the sum of individual digits of a EXPER. No. 2
positive integer.
PAGE No. 11
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-2

Objective:

CODE:

#include <stdio.h>
int main()
{
int integer, sum = 0, r = 0;
printf("Enter the positive number:");
scanf("%d", &integer);
while (integer > 0)
{
r = integer % 10;
sum = sum + r;
integer = integer / 10;
}
printf("The sum of individual digit of that integer is :%d", sum);
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to compute the sum of a geometric EXPER. No. 2
progression: 1+x+x^2+...+x^n.
PAGE No. 12
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-2

Objective:

CODE:

#include <stdio.h>
int main()
{
int x, n;
printf("Enter the value of x and n :");
scanf("%d%d", &x, &n);
int mul = 1, sum = 1;
if (n > 0)
{
for (int i = 0; i < n; i++)
{
mul = x * mul;
sum = sum + mul;
}
printf("The sum of series is :%d", sum);
}
else
{
printf("n cannot be negative.");
}
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to print a pyramid pattern of numbers. EXPER. No. 2
PAGE No. 13
DEPARTMENT OF
LABORATORY: Problem Solving with C Lab SEMESTER: 1st
School CSE

Experiment No-2

Objective:

CODE:

#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
for (int j = i - 1; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to find the largest and smallest EXPER. No. 3
numbers in a list of integers.
PAGE No. 14
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-3

Objective:

CODE:

#include <stdio.h>
int main()
{
int n;
printf("Enter the number of integers in your list :");
scanf("%d",&n);
int list[n];
printf("Enter those integers :");
for(int i=0;i<n;i++){
scanf("%d",&list[i]);
}
int max_list = list[0];
for (int i = 0; i < n; i++)
{
if (list[i] > max_list)
{
max_list = list[i];
}
}
printf("Maximum from that list is :%d\n", max_list);
int min_list = list[0];
for (int i = 0; i < n; i++)
{
if (list[i] < min_list)
{
min_list = list[i];
}
}
printf("Minimum from that list is :%d\n", min_list);

return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to perform addition and multiplication EXPER. No. 3
of two matrices.
PAGE No. 16
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-3

Objective:

CODE:

(I)
#include <stdio.h>
void input_matrices(int matrice[3][3])
{
printf("Enter the Values for the matrices:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &matrice[i][j]);
}
}
}
void add_matrices(int matrice_1[3][3], int matrice_2[3][3], int result_matrice[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result_matrice[i][j] = matrice_1[i][j] + matrice_2[i][j];
}
}
}
void print_result(int matrice[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
int main()
{
int matrix1[3][3], matrix2[3][3], result[3][3];
input_matrices(matrix1);
input_matrices(matrix2);
add_matrices(matrix1, matrix2, result);
printf("Resultant matrix after addition:\n");
print_result(result);
return 0;
}

( II )
#include <stdio.h>
void input_matrices(int matrice[3][3])
{
printf("Enter the Values for the matrices:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &matrice[i][j]);
}
}
}
void multipilication_matrices(int matrice_1[3][3], int matrice_2[3][3], int result_matrice[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
result_matrice[i][j] = result_matrice[i][j] + (matrice_1[i][k] * matrice_2[k][j]);
}
}
}
}
void print_result(int matrice[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
int main()
{
int matrix1[3][3], matrix2[3][3], result[3][3] = {0};
input_matrices(matrix1);
input_matrices(matrix2);
multipilication_matrices(matrix1, matrix2, result);
printf("Resultant matrix after multipilication:\n");
print_result(result);

return 0;
}

OUTPUT:

(I)

( II )
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to insert a sub-string into a main string EXPER. No. 4
at a given position.
PAGE No. 19
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-4

Objective:

CODE:

#include <stdio.h>
#include <string.h>
void insertSubstring(char mainStr[], char subStr[], int position) {
char result[200];
int i, j = 0;
for (i = 0; i < position; i++) {
result[i] = mainStr[i];
}
for (j = 0; subStr[j] != '\0'; j++) {
result[i + j] = subStr[j];
}
for (; mainStr[i] != '\0'; i++) {
result[i + j] = mainStr[i];
}
result[i + j] = '\0';
printf("Resultant String: %s\n", result);
}

int main() {
char mainStr[100], subStr[100];
int position;
printf("Enter the main string: ");
gets(mainStr);
printf("Enter the substring to insert: ");
gets(subStr);
printf("Enter the position to insert the substring: ");
scanf("%d", &position);
if (position < 0 || position > strlen(mainStr)) {
printf("Invalid position!\n");
} else {
insertSubstring(mainStr, subStr, position);
}

return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to count the lines, words, and EXPER. No. 4
characters in a given text.
PAGE No. 21
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-4

Objective:

CODE:

#include <stdio.h>
void countText() {
char text[1000];
int lines = 0, words = 0, characters = 0;
char ch;
printf("Enter the text (end with `~`):\n");
while ((ch = getchar()) != '~') {
characters++;
if (ch == '\n') {
lines++;
}
if (ch == ' ' || ch == '\n' || ch == '\t') {
words++;
}
}
if (characters > 0) {
words++;
lines++;
}
printf("Lines: %d\n", lines);
printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
}

int main() {
countText();
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to generate all prime numbers between EXPER. No. 5
1 and n.
PAGE No. 23
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-5

Objective:

CODE:

#include <stdio.h>
int isPrime(int num)
{
if (num <= 1)
{
return 0;
}
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers from 2 to %d are:\n", n);
for (int i = 2; i <= n; i++)
{
if (isPrime(i))
{
printf("%d ", i);
}
}
printf("\n");

return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program using recursion to find the factorial and EXPER. No. 5
GCD of numbers.
PAGE No. 25
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-5

Objective:

CODE:

(I)
#include <stdio.h>
int fact(int n)
{
int bp = 1;
if (n > 0)
{
bp = n * fact(n - 1);
return bp;
}
else
{
return 1;
}
}
int main()
{
int num;
printf("Enter the number num:");
scanf("%d", &num);
int result = fact(num);
printf("Factorial of %d is %d.", num, result);
return 0;
}
( II )
#include <stdio.h>
int GCD(int a, int b)
{
int bp = 1;
for (int i = 1; i <= a && i <= b; i++)
{
if (a % i == 0 && b % i == 0)
{
bp = i;
}
}
return bp;
}
int main()
{
int n1, n2;
printf("Enter n1 and n2 : ");
scanf("%d%d", &n1, &n2);
int result = GCD(n1, n2);
printf("The GCD of %d and %d id %d.", n1, n2, result);

return 0;
}

OUTPUT:

(I)

( II )
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to print the elements of an array in EXPER. No. 6
reverse order using pointer.
PAGE No. 27
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-6

Objective:

CODE:

#include <stdio.h>
void reverseArray(int *arr, int n) {
int *start = arr;
int *end = arr + n - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, n);
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to count vowels and consonants in a EXPER. No. 6
string using pointers.
PAGE No. 29
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-6

Objective:

CODE:

#include<stdio.h>
#include<string.h>
int main(){
char str[100];
printf("Enter the string:");
scanf("%[^\n]s",str);
int length=strlen(str);
int v_count=0,c_count=0,space_count=0;
for(int i=0;i<length;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'){
v_count++;
}
else if(str[i]==' '){
space_count++;
}
else{
c_count++;
}
}
printf("The number of vowels is %d and consunants is %d in the string.",v_count,c_count);
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to sort n array elements using pointers. EXPER. No. 6
PAGE No. 30
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-6
Objective:

CODE:

#include <stdio.h>
void sortArray(int *arr, int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (*(arr + j) > *(arr + j + 1)) {
temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}
}
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", (arr + i));
}
sortArray(arr, n);
printf("Sorted elements:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
return 0;
}
OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program for operations on complex numbers: EXPER. No. 7
read, write, addition, Multiplication.
PAGE No. 32
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-7

Objective:

CODE:

#include <stdio.h>
int main()
{
float real_1, img_1;
printf("Enter the real and imaginary part of the first complex number :");
scanf("%f%f", &real_1, &img_1);
printf("The first complex number is : %.2f + %.2fi\n", real_1, img_1);
float real_2, img_2;
printf("Enter the real and imaginary part of the second complex number :");
scanf("%f%f", &real_2, &img_2);
printf("The second complex number is : %.2f + %.2fi\n", real_2, img_2);
float r_real = 0, r_img = 0;
r_real = real_1 + real_2;
r_img = img_1 + img_2;
printf("The resultant complex number of those two complex number is ,after addition: %.2f+%.2fi\n", r_real,
r_img);
r_real = (real_1 * real_2) - (img_1 - img_2);
r_img = (real_1 * img_2) + (real_2 + img_1);
printf("The resultant complex number of those two complex number is ,after multipilication: %.2f+%.2fi\n",
r_real, r_img);
return 0;
}
OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program using structures to manage employee EXPER. No. 7
details and perform operations.
PAGE No. 34
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-7

Objective:

CODE:

#include <stdio.h>
int main()
{
for (int i = 0; i < 11; i++)
{
int e_number;
printf("Enter your number :");
scanf("%d", &e_number);
char e_name[100];
printf("Enter your name : ");
scanf(" %[^\n]", e_name);
int b_pay;
printf("Enter your basic pay :");
scanf("%d", &b_pay);
int j_year;
printf("Enter your joining year :");
scanf("%d", &j_year);
if (b_pay <= 5000)
{
b_pay = b_pay + (b_pay * 0.15);
}
else if (b_pay > 5000 && b_pay <= 25000)
{
b_pay = b_pay + (b_pay * 0.1);
}
if ((2024 - j_year) == 20)
{
printf("Your number is %d.\n", e_number);
printf("Your name is %s.\n", e_name);
printf("Your basic pay is %d.\n", b_pay);
printf("Your joining year is %d.\n", j_year);
}
else
{
printf("Your basic pay is %d.\n", b_pay);
}
}

return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to reverse the first n characters of a text EXPER. No. 8
file.
PAGE No. 36
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-8

Objective

CODE:

#include <stdio.h>
#include <string.h>
int main()
{
char text[1000];
printf("Enter the content of the text :");
scanf(" %[^\n]", text);
int n, length = 0;
length = strlen(text);
printf("Enter the value of n for reversing the text content :");
scanf("%d", &n);
int start = 0;
int end = 0;
while (start < n)
{
while (start < n && text[start] == ' ')
{
start++;
}
end = start;
while (end < n && text[end] != ' ')
{
end++;
}
int w_left = start, w_right = end - 1;
while (w_left < w_right) {
char temp = text[w_left];
text[w_left] = text[w_right];
text[w_right] = temp;
w_left++;
w_right--;
}
start = end;
}
printf("Reversed text : \n");
printf("%s", text);
return 0;
}

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to merge two files into a new file. EXPER. No. 8
PAGE No. 38
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-8

Objective

CODE:

#include <stdio.h>

#include <stdlib.h>

void mergeFiles(const char *file1, const char *file2, const char *outputFile) {

FILE *fptr1 = fopen(file1, "r");

FILE *fptr2 = fopen(file2, "r");

FILE *fptrOut = fopen(outputFile, "w");

char ch;

if (fptr1 == NULL || fptr2 == NULL || fptrOut == NULL) {

printf("Error opening files!\n");

exit(1);

// Read contents from first file and write to output file


while ((ch = fgetc(fptr1)) != EOF) {

fputc(ch, fptrOut);

// Read contents from second file and write to output file

while ((ch = fgetc(fptr2)) != EOF) {

fputc(ch, fptrOut);

printf("Files have been merged successfully!\n");

// Close all open files

fclose(fptr1);

fclose(fptr2);

fclose(fptrOut);

int main() {

char file1[100], file2[100], outputFile[100];

printf("Enter the name of the first file: ");

scanf("%s", file1);

printf("Enter the name of the second file: ");


scanf("%s", file2);

printf("Enter the name of the output file: ");

scanf("%s", outputFile);

mergeFiles(file1, file2, outputFile);

return 0;

OUTPUT:
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: Write a C program to develop a phone book app for managing EXPER. No. 9
contact details.
PAGE No. 39
DEPARTMENT OF SEMESTER:
LABORATORY: Problem Solving with C Lab
School CSE 1st

Experiment No-9

Objective

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 100

#define NAME_LEN 50

#define MOBILE_LEN 15

#define EMAIL_LEN 50

typedef struct {

char name[NAME_LEN];

char mobile[MOBILE_LEN];

char email[EMAIL_LEN];

} Contact;
Contact phoneBook[MAX];

int contactCount = 0;

void addContact() {

if (contactCount < MAX) {

printf("Enter name: ");

scanf("%s", phoneBook[contactCount].name);

printf("Enter mobile number: ");

scanf("%s", phoneBook[contactCount].mobile);

printf("Enter email: ");

scanf("%s", phoneBook[contactCount].email);

contactCount++;

printf("Contact added successfully.\n");

} else {

printf("Phone book is full.\n");

void displayContacts() {

for (int i = 0; i < contactCount; i++) {

printf("Contact %d:\n", i + 1);

printf("Name: %s\n", phoneBook[i].name);

printf("Mobile: %s\n", phoneBook[i].mobile);


printf("Email: %s\n", phoneBook[i].email);

void editContact() {

int index;

printf("Enter the contact number to edit: ");

scanf("%d", &index);

if (index > 0 && index <= contactCount) {

printf("Enter new name: ");

scanf("%s", phoneBook[index - 1].name);

printf("Enter new mobile number: ");

scanf("%s", phoneBook[index - 1].mobile);

printf("Enter new email: ");

scanf("%s", phoneBook[index - 1].email);

printf("Contact updated successfully.\n");

} else {

printf("Invalid contact number.\n");

void deleteContact() {

int index;

printf("Enter the contact number to delete: ");


scanf("%d", &index);

if (index > 0 && index <= contactCount) {

for (int i = index - 1; i < contactCount - 1; i++) {

phoneBook[i] = phoneBook[i + 1];

contactCount--;

printf("Contact deleted successfully.\n");

} else {

printf("Invalid contact number.\n");

int main() {

int choice;

while (1) {

printf("\nPhone Book Application\n");

printf("1. Add Contact\n");

printf("2. Display Contacts\n");

printf("3. Edit Contact\n");

printf("4. Delete Contact\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

addContact();

break;

case 2:

displayContacts();

break;

case 3:

editContact();

break;

case 4:

deleteContact();

break;

case 5:

exit(0);

default:

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

return 0;

Output :

You might also like