0% found this document useful (0 votes)
17 views52 pages

New Lab File Div

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)
17 views52 pages

New Lab File Div

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/ 52

Lab Record

of

Programming for Problem Solving


(CSN101)

BACHELOR OF TECHNOLOGY
In

COMPUTER SCIENCE AND ENGINEERING

Session 2023-24

Submitted to: - Submitted by: -

Dr. Prabhjot Kaur Name: Divyanshi Chauhan


Assistant Professor Roll No.:
School of Computing Sap ID: 1000024473
DIT University, Dehradun Class/Section: P1

SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by UGC)
Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.

August-December 2024
INDEX

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
CONDUCTION INSTRUCTOR’s
S.NO. NAME OF THE EXPERIMENTS
DATE SIGNATURE
Write a C program to demonstrate the programming
environment in C. Explain each basic entity required
1
to build a program, including the compilation and run
process.
Write the C programs to demonstrate the use of the
following operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
2
• Shorthand Assignment
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Comma Operator
Write a C program to convert a floating-point number
3
into the corresponding integer and vice versa.
Write a C program to convert degrees Fahrenheit into
4
degrees Celsius.
Write a C program to print the ASCII value of a
5
character.
Write a C program to find the greatest of three
6
numbers using && and if-else-if.
Write a C program to show the use of isalpha(),
7
isdigit(), isprint() and isspace() functions with if.
Write a C program to enter numbers from 1 to 7 and
8 display the corresponding day of the week using a
switch case statement.
Write a C program to calculate the sum of numbers
9
from m to n using a while loop.
Write a C program to display the square and cube of
10
first n natural numbers using the do-while loop.
Write the C programs to print the following patterns
using the for loop:
(i) (ii)
* 1
11
** 12
*** 123
**** 1234
***** 12345
Write the programs to demonstrate the use of break,
12
continue, and goto statements.
Develop a program to calculate the sum of n array
13
elements in C.

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Develop a program to find the largest array element in
14
C.
Develop a program to perform the multiplication of
15
two matrices.
Write the C programs to demonstrate the use of the
following string functions:
16
gets(), puts(), strlen(), strcmp(), strcpy(), strcat(),
strchr(), strstr(), strrev(), strlwr(), and strupr().
WAP to calculate the Power of a Number using a
17
function.
18 WAP to find the factorial of a number using recursion.
Write a C program to calculate the sum of two
19
numbers using a pointer.
Create a function to swap the values of two variables
20 demonstrating a call-by-value and a call-by-reference
concept.
Write a C program to
 Write a string in a file.
 Print contents in the reverse order of a file.
21
 Compare the contents of two files.
 Convert all characters in the UPPER CASE of
a File.
Write a program to read and display student
22
information using structure.

Experiment-01
Objective: Write a C program to demonstrate the programming environment in C.
Explain each basic entity required to build a program including the compilation and
run process
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Code:
//FIRST C PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();t
/*Printing address of
DIT University*/
printf("DIT University\n");
printf("Mussorie Diversion Road\n");
printf("Makkawala, Dehardun\n");
printf("Uttarakhand\t248009\tINDIA");
getch();
}

Output:

Experiment -02

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Objective: Write the C programs to demonstrate the use of the following operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Shorthand Assignment
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Comma Operator

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

//1. Arithmetic Operators

int a = 10, b = 5;

printf("Addition: %d + %d = %d\n", a, b, a + b);


printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);

//2. Relational Operators


int c = 10, d = 5;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("c == d: %d\n", c == d);
printf("c != d: %d\n", c != d);
printf("c > d: %d\n", c > d);
printf("c < d: %d\n", c < d);
printf("c >= d: %d\n", c >= d);
printf("c <= d: %d\n", c <= d);

//3. Logical Operators


int e = 10, f = 5, g = 0;

// Using logical AND (&&) with if


if (e > f && f > g) {
printf("Both conditions are true: e > f and f > g\n");
}

// Using logical OR (||) with if-else


if (e > f || f < g) {
printf("At least one condition is true: e > f or f < g\n");
} else {
printf("Both conditions are false: e <= f and f >= g\n");
}

// Using logical NOT (!) with if


if (!g) {
printf("g is zero, which is considered false, so !g is true\n");
}

//4.Shorthand Assignment Operators


int h = 10;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("Initial value of h: %d\n", h);
h += 5; // equivalent to h = h + 5
printf("After h += 5: %d\n", h);
h -= 3; // equivalent to h = h - 3
printf("After h -= 3: %d\n", h);
h *= 2; // equivalent to h = h * 2
printf("After h *= 2: %d\n", h);
h /= 4; // equivalent to h = h / 4
printf("After h /= 4: %d\n", h);
h %= 3; // equivalent to h = h % 3
printf("After h %%= 3: %d\n", h);

//5.Unary Operators

int i = 5;
int j = -i;

printf("Initial value of i: %d\n", i);


printf("Negation of i: %d\n", j);
printf("Increment of i: %d\n", ++i); // Pre-increment
printf("Decrement of i: %d\n", --i); // Pre-decrement

//6. Conditional Operator


int k = 15, l = 5;
int max;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
max = (k > l) ? k : l; // Ternary operator
printf("Max of %d and %d is %d\n", k, l, max);

//7. Bitwise Operators

int m = 15; // Binary: 1111


int n = 9; // Binary: 1001

printf("m & n: %d\n", m & n); // Bitwise AND


printf("m | n: %d\n", m | n); // Bitwise OR
printf("m ^ n: %d\n", m ^ n); // Bitwise XOR
printf("~m: %d\n", ~m); // Bitwise NOT
printf("n << 1: %d\n", n << 1); // Bitwise left shift
printf("n >> 1: %d\n", n >> 1); // Bitwise right shift

//8.Comma Operator

int o,p;

o = (p = 3, p + 2); // Comma operator

printf("Value of o: %d\n", o);


printf("Value of o: %d\n", p);

return 0;
}
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-03
Objective: Write a C program to convert a floating-point number into the corresponding
integer and vice versa.
Code:
#include <stdio.h>
#include <math.h>
int main() {
int choice;
float floatNum;
int intNum;
printf("Choose the conversion:\n");
printf("1. Convert float to int\n");
printf("2. Convert int to float\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
switch(choice) {

case 1:
printf("Enter a floating-point number: ");
scanf("%f", &floatNum);
intNum = (int)floatNum;
printf("Converted integer: %d\n", intNum);
break;

case 2:
printf("Enter an integer number: ");
scanf("%d", &intNum);
floatNum = (float)intNum;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("Converted floating-point number: %.2f\n", floatNum);
break;

default:
printf("Invalid choice!\n");
break;

}
return 0;
}

Output

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 04
Objective: Write a C program to convert degrees Fahrenheit into degrees Celsius.
Code:
#include <stdio.h>
int main() {
float elsiusit, elsius;
printf(“Enter temperature in Fahrenheit: “);
scanf(“%f”, &elsiusit);
elsius = (elsiusit – 32) *5/9;
printf(“%.2f Fahrenheit is equal to %.2f Celsius\n”, elsiusit, elsius);
return 0;
}
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-05
Objective: Write a C program to print the ASCII value of a character.

Code:

#include <stdio.h>

int main() {

char character;

printf("Enter a character: ");

scanf("%c", &character);

printf("The ASCII value of '%c' is %d\n", character, (int) character);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-06
Objective: Write a C program to find the greatest of three numbers using && and if-else-if

Code:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}
return 0;
}
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-07
Objective : Write a C program to show the use of isalpha(), isdigit(), isprint() and isspace()
functions with if.
Code:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isalpha(ch)) {
printf("The character '%c' is an alphabet.\n", ch);
} else {
printf("The character '%c' is not an alphabet.\n", ch);
}
if (isdigit(ch)) {
printf("The character '%c' is a digit.\n", ch);
} else {
printf("The character '%c' is not a digit.\n", ch);
}
if (isprint(ch)) {
printf("The character '%c' is printable.\n", ch);
} else {
printf("The character '%c' is not printable.\n", ch);
}
if (isspace(ch)) {
printf("The character '%c' is a whitespace character.\n", ch);
} else {
printf("The character '%c' is not a whitespace character.\n", ch);

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
}
return 0;
}

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-08
Objective : Write a C program to enter numbers from 1 to 7 and display the corresponding
day of the week using a switch case statement.

Code:

#include <stdio.h>

int main() {

int day;

printf("Enter a number (1-7): ");

scanf("%d", &day);

switch (day) {

case 1:

printf("Sunday\n");

break;

case 2:

printf("Monday\n");

break;

case 3:

printf("Tuesday\n");

break;

case 4:

printf("Wednesday\n");

break;

case 5:

printf("Thursday\n");

break;

return 0;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
}

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment-09
Objective: Write a C program to calculate the sum of numbers from m to n using a while
loop.

Code:

#include <stdio.h>

int main() {

int m, n, sum = 0;

printf("Enter the starting number (m): ");

scanf("%d", &m);

printf("Enter the ending number (n): ");

scanf("%d", &n);

int current = m;

while (current <= n) {

sum += current;

current++;

printf("The sum of numbers from %d to %d is: %d\n", m, n, sum);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 10
Objective: Write a C program to display the square and cube of first n natural numbers using
the do-while loop.
Code:
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter the value of n: ");
scanf("%d", &n);
do {
int square = i * i;
int cube = i * i * i;
printf("Number: %d, Square: %d, Cube: %d\n", i, square, cube);
i++;
} while (i<= n);
return 0;
}

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 11
Objective: Write the C programs to print the following patterns using the for loop:

(i) (ii)
* 1
** 12
*** 123
**** 1234
***** 12345
Code:

i)

#include <stdio.h>

int main() {

int i, j;

for (i = 1; i <= 5; i++) {

for (j = 1; j <= i; j++) {

printf("*");

printf("\n");

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
ii)

#include <stdio.h>

int main() {

int i, j, k;

for (i = 1; i <= 5; i++) {

for (j = 1; j <= 5 - i; j++) {

printf(" ");

for (k = 1; k <= i; k++) {

printf("%d", k);

printf("\n");

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 12
Objective: Write the programs to demonstrate the use of break, continue, and goto
statements.

Code:

//Use Of Break

#include <stdio.h>

int main() {

for (int i = 1; i <= 10; i++) {

if (i == 7) {

break;

printf("%d\n", i);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
//Use Of Continue

#include <stdio.h>

int main() {

for (int i = 1; i <= 10; i++) {

if (i == 5) {

continue;

printf("%d\n", i);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
//Use Of Goto

#include <stdio.h>

int main() {

int i = 1;

printf("Demonstrating goto statement:\n");

while (i <= 10) {

if (i == 5) {

printf("Using goto to skip to the end of the loop at i = %d\n", i);

goto end;

printf("%d ", i);

i++;

end:

printf("\nLoop ended using goto.\n");

return 0;

}
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 13
Objective: Develop a program to calculate the sum of n array elements in C.

Code:

#include <stdio.h>

int main()

int n, i, sum = 0;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

for (i = 0; i< n; i++) {

scanf("%d", &arr[i]);

for (i = 0; i< n; i++) {

sum += arr[i];

printf("The sum of the array elements is: %d\n", sum);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 14

Objective: Develop a program to find the largest array element in C.

Code:

#include <stdio.h>

int main()

int n, i, largest;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

for (i = 0; i< n; i++) {

scanf("%d", &arr[i]);

largest = arr[0];

for (i = 1; i< n; i++) {

if (arr[i] > largest) {

largest = arr[i];

printf("The largest element in the array is: %d\n", largest);

return 0;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 15
Objective: Develop a program to perform the multiplication of two matrices.

Code:

#include <stdio.h>

int main()

int r1, c1, r2, c2, i, j, k;

printf("Enter the number of rows and columns of the first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter the number of rows and columns of the second matrix: ");

scanf("%d %d", &r2, &c2);

if (c1 != r2) {

printf("Matrix multiplication is not possible!\n");

return 0;

int mat1[r1][c1], mat2[r2][c2], result[r1][c2];

printf("Enter elements of the first matrix:\n");

for (i = 0; i< r1; i++) {

for (j = 0; j < c1; j++) {

scanf("%d", &mat1[i][j]);

printf("Enter elements of the second matrix:\n");

for (i = 0; i< r2; i++) {

for (j = 0; j < c2; j++) {

scanf("%d", &mat2[i][j]);

}
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
}

for (i = 0; i< r1; i++) {

for (j = 0; j < c2; j++) {

result[i][j] = 0;

for (i = 0; i< r1; i++) {

for (j = 0; j < c2; j++) {

for (k = 0; k < c1; k++) {

result[i][j] += mat1[i][k] * mat2[k][j];

printf("The result of matrix multiplication is:\n");

for (i = 0; i< r1; i++) {

for (j = 0; j < c2; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 16
Objective: Write the C programs to demonstrate the use of the following string functions:
gets(), puts(), strlen(), strcmp(), strcpy(), strcat(), strchr(), strstr(), strrev(), strlwr(), and
strupr().

Code:

i) gets() & ii) puts()

#include<stdio.h>

#include <string.h>

int main(){

char name[50];

printf("Enter your name: ");

gets(name); //reads string from user

printf("Your name is: ");

puts(name); //displays string

return 0;

Output:

iii) strlen()

#include <stdio.h>

#include <string.h>

int main()

char a[20]="Divyanshi";
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("Length of string a = %d \n",strlen(a));

return 0;

}
Output:

iv) strcmp()

#include <stdio.h>

#include <string.h>

int main() {

char str1[50] = "apple";

char str2[50] = "orange";

if (strcmp(str1, str2) == 0)

printf("The strings are equal.\n");

else

printf("The strings are not equal.\n");

return 0;

}
Output :

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
v) strcpy()

#include <stdio.h>

#include <string.h>

int main() {

char str1[20] = "Divyanshi Chauhan";

char str2[20];

// copying str1 to str2

strcpy(str2, str1);

puts(str2); // C programming

return 0;

Output:

vi) strcat()

#include <stdio.h>

#include <string.h>

int main() {

char str1[100] = "This is ", str2[] = "lab file of divyanshi chauhan.";

// concatenates str1 and str2

// the resultant string is stored in str1.


Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
strcat(str1, str2);

puts(str1);

return 0;

Output:

vii) strchr()

#include <stdio.h>

#include <string.h>

int main() {

char str[] = "Divyanshi Chauhan";

char *ptr = strchr(str, 'y');

if (ptr != NULL) {

printf("'%c' found at position %ld.\n", *ptr, ptr - str);

} else {

printf("'%c' not found.\n", 'o');

return 0;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

viii) strstr()

#include <stdio.h>

#include <string.h>

int main() {

char str1[] = "Lab file of Divyanshi Chauhan";

char str2[] = "Divyanshi";

char *result = strstr(str1, str2);

if (result != NULL) {

printf("Substring found at index %d\n", result - str1);

} else {

printf("Substring not found\n");

return 0;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

ix) strrev()

#include <stdio.h>

#include <string.h>

int main() {

char str[50] = "divyanshi";

strrev(str);

printf("Reversed string: %s\n", str);

return 0;

Output:

x) strlwr()

#include<string.h>

int main()

char str[ ] = “DIVYANSHI CHAUHAN”;

// converting the given string into lowercase.

Printf(“%s\n”,strlwr (str));

return 0;

}
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

xi) strupr()

#include<stdio.h>

#include<string.h>

int main()

char str[ ] = “divyanshi hauhan”;

// converting the given string into uppercase.

Printf(“%s\n”,strupr (str));

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 17
Objective : WAP to calculate the Power of a Number using a function.

Code:

#include <stdio.h>

int power(int, int);

void main()

int num, pow, res;

printf("Enter any number : ");

scanf("%d", &num);

printf("Enter power of number : ");

scanf("%d", &pow);

res = power(num, pow);

printf("%d's power %d = %d", num, pow, res);

int power(int n, int p)

{ int r = 1;

while (p >= 1)

r = r * n;

p--;

return r;

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 18
Objective : WAP to find the factorial of a number using recursion.

Code:

#include<stdio.h>

int factorial(int n);

int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, factorial(n));

return 0;

int factorial(int n) {

if (n>=1)

return n*factorial(n-1);

else

return 1;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 19
Objective: Write a C program to calculate the sum of two numbers using a pointer.

Code :

#include <stdio.h>

int main()

int first, second, *p, *q, sum;

printf("Enter two integers to add\n");

scanf("%d%d", &first, &second);

p = &first;

q = &second;

sum = *p + *q;

printf("Sum of the numbers = %d\n", sum);

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 20
Objective : Create a function to swap the values of two variables demonstrating a call-by-
value and a call-by-reference concept.

Code:
i) Call By Value
#include <stdio.h>

void swapByValue(int a, int b) {

int temp = a;

a = b;

b = temp;

printf("Inside swapByValue - a: %d, b: %d\n", a, b);

int main() {

int x = 5, y = 10;

printf("Before swapByValue - x: %d, y: %d\n", x, y);

swapByValue(x, y);

printf("After swapByValue - x: %d, y: %d\n", x, y); // x and y remain unchanged

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
ii) Call By Reference

Code:
#include <stdio.h>

void swapByReference(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

printf("Inside swapByReference - a: %d, b: %d\n", *a, *b);

int main() {

int x = 5, y = 10;

printf("Before swapByReference - x: %d, y: %d\n", x, y);

swapByReference(&x, &y);

printf("After swapByReference - x: %d, y: %d\n", x, y); // x and y are swapped

return 0;

Output :

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 21
Objective: Write a C program to

 Write a string in a file.

 Print contents in the reverse order of a file.

 Compare the contents of two files.

 Convert all characters in the UPPER CASE of a File.

Code:
#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#define FILE1 "file1.txt"

#define FILE2 "file2.txt"

// Function to write a string to a file

void writeStringToFile(const char *filename, const char *content) {

FILE *file = fopen(filename, "w");

if (file == NULL) {

perror("Error opening file for writing");

return;

fputs(content, file);

fclose(file);

printf("Successfully wrote to %s\n", filename);

// Function to print the contents of a file normally

void printFileContents(const char *filename) {

FILE *file = fopen(filename, "r");


Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
if (file == NULL) {

perror("Error opening file");

return;

int ch;

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

putchar(ch);

putchar('\n');

fclose(file);

// Function to print the contents of a file in reverse order

void printFileInReverse(const char *filename) {

FILE *file = fopen(filename, "r");

if (file == NULL) {

perror("Error opening file");

return;

fseek(file, 0, SEEK_END);

long fileSize = ftell(file);

printf("File contents in reverse order:\n");

for (long i = fileSize - 1; i >= 0; i--) {

fseek(file, i, SEEK_SET);

putchar(fgetc(file));

putchar('\n');

fclose(file);
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
}

// Function to compare the contents of two files

int compareFiles(const char *file1, const char *file2) {

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

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

if (f1 == NULL || f2 == NULL) {

perror("Error opening file(s) for comparison");

return -1;

int ch1, ch2;

int areEqual = 1;

while ((ch1 = fgetc(f1)) != EOF && (ch2 = fgetc(f2)) != EOF) {

if (ch1 != ch2) {

areEqual = 0;

break;

if (fgetc(f1) != EOF || fgetc(f2) != EOF) {

areEqual = 0;

fclose(f1);

fclose(f2);

return areEqual;

// Function to convert all characters in a file to uppercase

void convertFileToUpper(const char *filename) {

FILE *file = fopen(filename, "r+");


Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
if (file == NULL) {

perror("Error opening file");

return;

int ch;

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

if (islower(ch)) {

fseek(file, -1, SEEK_CUR); // Move back one position

fputc(toupper(ch), file);

fseek(file, 0, SEEK_CUR); // Move forward to continue reading

fclose(file);

int main() {

// 1. Write a string to a file

const char *content = "Divyanshi Chauhan";

writeStringToFile(FILE1, content);

printf("Content written to %s: %s\n", FILE1, content);

// 2. Print contents in reverse order

printFileInReverse(FILE1);

// 3. Compare contents of two files

writeStringToFile(FILE2, content); // Creating file2 with the same content for testing

int result = compareFiles(FILE1, FILE2);

if (result == 1) {
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("The files %s and %s are identical.\n", FILE1, FILE2);

} else if (result == 0) {

printf("The files %s and %s are different.\n", FILE1, FILE2);

// 4. Convert all characters to uppercase in file1

convertFileToUpper(FILE1);

printf("File %s after converting to uppercase:\n", FILE1);

printFileContents(FILE1); // Display contents in normal order

return 0;

Output:

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 22

Objective: Write a program to read and display student information using structure.

Code:

#include <stdio.h>

struct Student {

char name[50];

int SAPID;

char section[50];

char subject[50];

float marks;

};

int main() {

struct Student student;

// Read student information

printf("Enter name: ");

scanf(" %[^\n]s", student.name);

printf("Enter SAP ID: ");

scanf("%d", &student.SAPID);

printf("Enter Section: ");

scanf(" %[^\n]s", student.section);

printf("Enter subject: ");

scanf(" %[^\n]s", student.subject);

printf("Enter marks: ");

scanf("%f", &student.marks);

// Display student information

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("\nStudent Information:\n");

printf("Name: %s\n", student.name);

printf("SAP ID: %d\n", student.SAPID);

printf("Section: %s\n", student.section);

printf("Subject: %s\n", student.subject);

printf("Marks: %.2f\n", student.marks);

return 0;

Output :

Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing

You might also like