ST.
ANDREWS INSTITUTE
OF TECHNOLOGY & MANAGEMENT
Gurgaon Delhi (NCR)
Approved by AICTE, Govt. of India, New Delhi Affiliated to
Maharshi Dayanand University
‘A’ Grade State University, accredited by NAAC
Session: 2024 – 2025
Bachelor of Technology
(AIML) Practical File - ESC-
CSE-103G
Submitted to: Submitted By :
Ms. Shagun Goyal Name: Dinesh Kumar
(Assistant Professor) Roll No.: 247001
Applied Science Department
Practical-1:- Write a C program to print your bio data including 7-8 lines at
least.
Program:-
#include <stdio.h>
int main() {
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
char fathersName[] = "Samer Singh";
char address[] = "Bhiwani Hyaryana";
char phone[] = "+91 8950932595";
char email[] = "work.dinesh@gmail.com";
char dob[] = "07/04/2004";
char education[] = "Pursuing B.Tech in CSE with
specialization in AI/ML";
char skills[] = "C, Python, JavaScript, HTML5, CSS3";
char hobbies[] = "Music, Finance, Traveling, Coding,
Volleyball";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
// Print bio-data
printf(" BIO-DATA \n");
printf("============================================\n");
printf("Name: %s\n", name);
printf("Father's Name: %s\n", fathersName);
printf("Address: %s\n", address);
printf("Phone: %s\n", phone);
printf("Email: %s\n", email);
printf("Date of Birth: %s\n", dob);
printf("Education: %s\n", education);
printf("Skills: %s\n", skills);
printf("Hobbies: %s\n", hobbies);
printf("============================================\n");
return 0;
}
Output:-
Practical-2:- The distance between two cities (in kms) is input through the
keyboard. Write a program to convert and print this distance in
meters, feet, inches and centimeters.
Program:-
#include <stdio.h>
int main()
{
char name[] = "Dinesh kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
float distance_km;
const float km_to_m = 1000.0;
const float km_to_feet = 3280.84;
const float km_to_inches = 39370.1;
const float km_to_cm = 100000.0;
printf("Enter distance between two cities (in kilometers):
");
scanf("%f", &distance_km);
float distance_m = distance_km * km_to_m;
float distance_feet = distance_km * km_to_feet;
float distance_inches = distance_km * km_to_inches;
float distance_cm = distance_km * km_to_cm;
printf("\nDistance in different units:\n");
printf("Distance in meters: %.2f m\n", distance_m);
printf("Distance in feet: %.2f ft\n", distance_feet);
printf("Distance in inches: %.2f in\n", distance_inches);
printf("Distance in centimeters: %.2f cm\n", distance_cm);
printf(" \n");
return 0;
}
Output:-
Practical-3:- Consider a currency system in which there are notes of seven
denominations, namely, Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100. If
the change given to Pranav Rs. N is input, write a program to
compute smallest number of notes that will combine to give Rs. N.
Program:-
#include <stdio.h>
int main() {
// Print the name and roll number
printf("Name: Dinesh kumar\n");
printf("Roll Number: 247001\n\n");
int denominations[] = {100, 50, 10, 5, 2, 1};
int num_notes[6] = {0};
int N;
printf("Enter the amount (Rs. N): ");
scanf("%d", &N);
for (int i = 0; i < 6; i++)
{
if (N >= denominations[i])
{
num_notes[i] = N / denominations[i];
N = N % denominations[i];
}
}
printf("\nThe smallest number of notes required are:\n");
for (int i = 0; i < 6; i++)
{
if (num_notes[i] > 0)
{
printf("Rs. %d: %d notes\n", denominations[i],
num_notes[i]);
}
}
return 0;
}
Output:-
Practical-4:- Ram is watching the children playing in a park. He wants to write a
program that will get height of a child from the user and decide
whether the child is
normal, tall, or short based on the below criteria:-
Height less than 2.5
feet short, 2.5 - 4
feet normal; 4-5.5feet
-> tall
Progra
m:-
#include <stdio.h>
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
float height;
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("Enter the height of the child (in feet): ");
scanf("%f", &height);
if (height < 2.5)
{
printf("The child is short.\n");
}
else if (height >= 2.5 && height < 4.0)
{
printf("The child is normal.\n");
}
else if (height >= 4.0 && height <= 5.5)
{
printf("The child is tall.\n");
}
else
{
printf("Height is out of the expected range.\n");
}
return 0;
}
Output:-
Practical-5:- Write a C program to print the following pattern.
1
11
121
1331
1464
1
Program:-
include
<stdio.h> int
main() {
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
int rows = 5;
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
// Print Pascal's Triangle
for (int i = 0; i < rows; i++) {
int coef = 1; // Coefficient for the current row
// Print leading spaces for formatting
for (int space = 0; space < rows - i - 1; space++) {
printf(" ");
}
// Print coefficients for the current row
for (int j = 0; j <= i; j++) {
printf("%d ", coef);
coef = coef * (i - j) / (j + 1);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Output:-
Practical-6:- Write a program to display all the factors of the given number.
Program:-
#include <stdio.h>
int main()
{
char name[] = "Dinesh";
char rollNo[] = "247001";
int number;
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
// Input the number from the user
printf("Enter a number to find its factors: ");
scanf("%d", &number);
// Print the factors of the number
printf("Factors of %d are: ", number);
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Output:-
Practical-7:- consider an array, A=[1,5,6,8,3,2,1,0]. Write a C
function that returns the index of number 6 in the
passed array A. (Linear Search)
Program:-
#include <stdio.h>
// Function to perform linear search
int linearSearch(int arr[], int size, int target)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == target)
{
return i; // Return the index if the target is
found
}
}
return -1; // Return -1 if the target is not found
}
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
int A[] = {1, 5, 6, 8, 3, 2, 1, 0};
int size = sizeof(A) / sizeof(A[0]);
int target = 6;
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
// Call the linear search function
int index = linearSearch(A, size, target);
// Print the result
if (index != -1)
{
printf("The index of number %d in the array is:
%d\n", target, index);
}
else
{
printf("Number %d is not found in the array.\n",
target);
}
return 0;
}
Output:-
Practical-8:-Write a program to find location of an element in 1-D array.
Program:-
#include <stdio.h>
int find_element_location(int array[], int size, int target)
{
for (int i = 0; i < size; i++)
{
if (array[i] == target)
{
return i;
}
}
return -1; // Return -1 if the element is not found
}
int main()
{
int array[] = {10, 20, 30, 40, 50};
int size = sizeof(array) / sizeof(array[0]);
int target;
// Print your name and roll number
printf("Name: Dinesh Kumar\n");
printf("Roll No: 247001\n");
printf("\n");
printf("Enter the element to find: ");
scanf("%d", &target);
// Find the location of the target element
int location = find_element_location(array, size,
target);
if (location != -1)
{
printf("Element %d found at index %d.\n", target,
location);
}
else
{
printf("Element %d not found in the array.\n",
target);
}
return 0;
}
Output:-
Practical-9:- Write a program to do matrix addition of two matrices input
by user of dimensions 3*4
Program:-
#include <stdio.h>
int main() {
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
int matrix1[3][4], matrix2[3][4], sum[3][4];
int i, j;
// Input first matrix
printf("Enter elements of the first 3x4 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input second matrix
printf("Enter elements of the second 3x4 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Performing addition
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Result
printf("\nResultant matrix after addition:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:-
Practical-10:- Write a program to implement any 6 inbuilt string functions in
C on given input string.
Program:-
Below , C program demonstrating the use of 6 inbuilt string
functions (strlen, strcpy, strcat, strcmp, strchr, and strrev
#include <stdio.h>
#include <string.h>
// Function to reverse a string (manual implementation of
strrev)
void reverseString(char *str)
{
int len = strlen(str);
for (int i = 0; i < len / 2; i++)
{
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main()
{
char name[] = "Dinesh kumar";
char rollNo[] = "247001";
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
char str1[100], str2[100], str3[200], ch;
int comparisonResult;
// Input string from user
printf("Enter the first string: ");
gets(str1); // Using gets for simplicity (be cautious of
buffer overflow)
printf("Enter the second string: ");
gets(str2);
// 1. strlen: Get the length of the first string
printf("\nLength of the first string: %lu", strlen(str1));
// 2. strcpy: Copy the first string into a new string
strcpy(str3, str1);
printf("\nString after copying (strcpy): %s", str3);
// 3. strcat: Concatenate second string to the first string
strcat(str3, str2);
printf("\nString after concatenation (strcat): %s", str3);
// 4. strcmp: Compare two strings
comparisonResult = strcmp(str1, str2);
if (comparisonResult == 0)
printf("\nStrings are equal (strcmp).");
else if (comparisonResult > 0)
printf("\nFirst string is greater than second string
(strcmp).");
else
printf("\nFirst string is smaller than second string
(strcmp).");
// 5. strchr: Find the first occurrence of a character in
the first string
printf("\nEnter a character to find in the first string:
");
scanf(" %c", &ch);
char *position = strchr(str1, ch);
if (position)
printf("\nCharacter '%c' found at position: %ld
(strchr)", ch, position - str1 + 1);
else
printf("\nCharacter '%c' not found (strchr).", ch);
// 6. strrev: Reverse the first string
reverseString(str1); // Using custom reverse function
printf("\nFirst string after reversal (strrev): %s\n",
str1);
return 0;
}
Output:-
$ practical_
Practical-11:- Write a program to reverse the given input string without
using string functions.
Program:-
#include <stdio.h>
int main() {
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
printf("Name: %s\n", name);
printf("Roll No: %s\n", rollNo);
printf("\n");
char str[100], reversed[100];
int length = 0, i;
// Input string from user
printf("Enter a string: ");
gets(str); // Using gets for simplicity (be cautious of
buffer overflow)
// Calculate the length of the string manually
while (str[length] != '\0') {
length++;
}
// Reverse the string
for (i = 0; i < length; i++) {
reversed[i] = str[length - i - 1];
}
reversed[length] = '\0'; // Null-terminate the reversed
string
// Output the reversed string
printf("Reversed string: %s\n", reversed);
return 0;
}
Output:-
Practical-12:- Write a C program to implement a function to print upto the
nth Fibonacci element in the series using recursion.
Program:-
#include <stdio.h>
// Function to calculate the nth Fibonacci number using
recursion
int fibonacci(int n)
{
if (n <= 1)
return n; // Base case:
F(0) = 0, F(1) = 1
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive
case
}
// Function to print Fibonacci series up to the nth element
void printFibonacciSeries(int n)
{
printf("Fibonacci Series up to %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
printf("\n");
}
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
int n;
printf("Enter the number of elements in the Fibonacci
series: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Please enter a positive integer.\n");
}
else
{
printFibonacciSeries(n);
}
return 0;
}
Output:-
Practical-13:- Write a C program to find:
a. the square root of a number using the sqrt() function
b. the log value using log() function
c. the power using pow() function
Program:-
#include <stdio.h>
#include <math.h> // Required for sqrt(), log(), and pow()
int main()
{
char name[] = "Dinesh kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
double num, base, exponent;
// a. Square root of a number
printf("Enter a number to find its square root: ");
scanf("%lf", &num);
if (num < 0)
{
printf("Square root of negative numbers is not defined
in real numbers.\n");
}
else
{
printf("Square root of %.2lf is %.2lf\n", num,
sqrt(num));
}
// b. Logarithm (natural log) of a number
printf("\nEnter a number to find its natural logarithm:
");
scanf("%lf", &num);
if (num <= 0)
{
printf("Logarithm is not defined for non-positive
numbers.\n");
}
else
{
printf("Natural logarithm of %.2lf is %.2lf\n", num,
log(num));
}
// c. Power of a number
printf("\nEnter the base number: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%lf", &exponent);
printf("%.2lf raised to the power of %.2lf is %.2lf\n",
base, exponent, pow(base, exponent));
return 0;
}
Output:-
Practical-14:- i Write a program to swap two numbers using call by value.
II Write a program to swap two numbers using call by
reference
Program For I :-
#include <stdio.h>
// Function to swap two numbers (call by value)
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()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001
";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swapByValue(num1, num2); // Call by value
printf("After swapByValue: num1 = %d, num2 = %d\n", num1,
num2);
return 0;
}
Output FOR I:-
25
Program For II :-
#include <stdio.h>
// Function to swap two numbers (call by reference)
void swapByReference(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swapByReference(&num1, &num2); // Call by reference
printf("After swapByReference: num1 = %d, num2 = %d\n",
num1, num2);
return 0;
}
Output for II:-
Practical-15:- Write a C program to implement a function to print upto the
nth Fibonacci element in the series using recursion.
Program:-
#include <stdio.h>
#include <string.h>
// Define a structure to store student details
struct Student
{
char name[50];
int rollNumber;
float marks;
};
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
struct Student student;
// Input student details
printf("Enter student's name: ");
fgets(student.name, sizeof(student.name), stdin);
student.name[strcspn(student.name, "\n")] = '\0'; // Remove
trailing newline
printf("Enter student's roll number: ");
scanf("%d", &student.rollNumber);
printf("Enter student's marks: ");
scanf("%f", &student.marks);
// Display student details
printf("\nStudent Details:\n");
printf("Name : %s\n", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Marks : %.2f\n", student.marks);
return 0;
}
Output:-
Practical-16:- Write a program to read a text file and display its contents in
C.
Program:-
#include <stdio.h>
int main()
{
char name[] = "Dinesh Kumar";
char rollNo[] = "247001";
// Print name and roll number
printf("Name: %s\n", name);
printf("Roll No: %s\n\n", rollNo);
FILE *file; // File pointer
char filename[100]; // Array to store the file name
char ch; // Variable to hold each character
// Ask the user for the file name
printf("Enter the name of the file to read: ");
scanf("%s", filename);
// Open the file in read mode
file = fopen(filename, "r");
if (file == NULL)
{ // Check if the file exists
printf("Error: Could not open file %s\n", filename);
return 1;
}
printf("\nContents of the file %s:\n", filename);
// Read and display the file character by character
while ((ch = fgetc(file)) != EOF)
{
putchar(ch);
}
// Close the file
fclose(file);
return 0;
}
Output:-