New Lab File Div
New Lab File Div
of
BACHELOR OF TECHNOLOGY
In
Session 2023-24
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() {
int a = 10, b = 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);
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;
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);
//8.Comma Operator
int o,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;
scanf("%c", &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;
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;
scanf("%d", &m);
scanf("%d", &n);
int current = m;
sum += current;
current++;
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;
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;
printf(" ");
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() {
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() {
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;
if (i == 5) {
goto end;
i++;
end:
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;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
sum += arr[i];
return 0;
Output:
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
Experiment – 14
Code:
#include <stdio.h>
int main()
int n, i, largest;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
largest = arr[0];
largest = arr[i];
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()
printf("Enter the number of rows and columns of the first matrix: ");
printf("Enter the number of rows and columns of the second matrix: ");
if (c1 != r2) {
return 0;
scanf("%d", &mat1[i][j]);
scanf("%d", &mat2[i][j]);
}
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
}
result[i][j] = 0;
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:
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
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() {
if (strcmp(str1, str2) == 0)
else
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 str2[20];
strcpy(str2, str1);
puts(str2); // C programming
return 0;
Output:
vi) strcat()
#include <stdio.h>
#include <string.h>
int main() {
puts(str1);
return 0;
Output:
vii) strchr()
#include <stdio.h>
#include <string.h>
int main() {
if (ptr != NULL) {
} else {
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() {
if (result != NULL) {
} else {
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() {
strrev(str);
return 0;
Output:
x) strlwr()
#include<string.h>
int main()
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()
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>
void main()
scanf("%d", &num);
scanf("%d", &pow);
{ 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 main() {
int n;
scanf("%d",&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()
p = &first;
q = &second;
sum = *p + *q;
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>
int temp = a;
a = b;
b = temp;
int main() {
int x = 5, y = 10;
swapByValue(x, y);
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>
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swapByReference(&x, &y);
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
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
if (file == NULL) {
return;
fputs(content, file);
fclose(file);
return;
int ch;
putchar(ch);
putchar('\n');
fclose(file);
if (file == NULL) {
return;
fseek(file, 0, SEEK_END);
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
}
return -1;
int areEqual = 1;
if (ch1 != ch2) {
areEqual = 0;
break;
areEqual = 0;
fclose(f1);
fclose(f2);
return areEqual;
return;
int ch;
if (islower(ch)) {
fputc(toupper(ch), file);
fclose(file);
int main() {
writeStringToFile(FILE1, content);
printFileInReverse(FILE1);
writeStringToFile(FILE2, content); // Creating file2 with the same content for testing
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) {
convertFileToUpper(FILE1);
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() {
scanf("%d", &student.SAPID);
scanf("%f", &student.marks);
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing
printf("\nStudent Information:\n");
return 0;
Output :
Student’s Name: Divyanshi Chauhan SAP ID: 1000024473 B. Tech (1st Semester, Section P1)
School of Computing