C Lab II Ece Eee 2024 2025
C Lab II Ece Eee 2024 2025
LIST OF EXPERIMENTS
COURSE OUTCOMES:
At the end of the course, the students will be able to:
AIM
1.A : To write a C program to illustrate the concept of Statements and Expressions.
PROGRAM
#include <stdio.h>
int main()
{
float radius;
double area, circumference;
printf("\n Enter the radius of the circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius; circumference=2*3.14*radius;
printf(" \n Area = %lf", area);
printf(" \n Circumference = %lf", circumference);
return 0;
}
OUTPUT
Enter the radius of the circle : 3Area = 28.260000
Circumference = 18.840000
ALGORITHM
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
PROGRAM
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculating sum
sum = number1 + number2;
printf("Sum of two numbers: ");
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
OUTPUT
Enter two integers: 4 5
Sum of two numbers: 4 + 5 = 9
RESULT: Thus the C Program to illustrate concept of Statements and Expressions has been executed.
To write a C Program to illustrate the concept of Decision making and Branching statements.
C supports decision control statements that can alter the flow of a sequence of instructions. These statements help
to jump from one part of the program to another depending on whether a particularcondition is satisfied or not.
if statement,
if–else statement,
switch–case statement.
Step 6: Exit
PROGRAM
int a;
if(a%2==0)
OUTPUT
6 is even
B. IF–ELSE–IF STATEMENT
C Program to check whether a number is negative, positive or zero using if–else–if Statement
ALGORITHM:
1. Input a number from the user.
5. End.
PROGRAM
#include <stdio.h>
int main()
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");
else if(num>0)
printf("\n The number is positive");
else
printf("\n The number is negative");
return 0;
}
OUTPUT:
C. SWITCH–CASE STATEMENT
PROGRAM:
#include <stdio.h>
int main()
{
char c;
printf("Enter an Alphabet\n");
scanf("%c", &c);
switch(c) {case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is VOWEL", c);break;
default:
printf("%c is CONSONANT", c);
}
return 0;
}
OUTPUT
Enter an Alphabete
e is VOWEL Enter an AlphabetZ
Z is CONSONANT
RESULT:
Thus the C Program to illustrate the concept of Decision making and Branching statements.
DESCRIPTION
Iterative statements are used to repeat the execution of a sequence of statements until the specifiedexpression
becomes false. C supports three types of iterative statements also known as looping statements. They are
while loop
do–while loop
for loop
A.WHILE LOOP
ALGORITHM
Step 1: Start
PROGRAM
#include <stdio.h>
int main()
{
int i = 1,n;
printf("Enter the Number");
scanf("%d",&n);
while(i<=n)
{
return 0;
OUTPUT:
PROGRAM
#include <stdio.h>
int main()
{
int i = 1;do
{
printf("\t %d", i);
i = i + 1;
}
while(i<=10);
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
{
sum = sum + i;
i = i + 1;
}
while(i<=n);
avg = (float)sum/n;
printf("\n The sum of first %d numbers = %d",n, sum);
printf("\n The average of first %d numbers = %.2f", n, avg);
return 0;
}
OUTPUT
C. FOR LOOP
PROGRAM
#include <stdio.h>
int main()
{
int i, n;
printf("\n Enter the value of n :");
scanf("%d", &n);
for(i=1;i<=n;i++)
printf("\n %d", i);
return 0;
}
OUTPUT:
Enter the value of n : 51
2
3
4
5
RESULT: Thus the C Program to illustrate Iteration using Looping Statements was executed.
factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f
#include<stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
Output
Enter a positive integer: 6Factorial of 6 = 720
Ex.No:2.2 ARRAYS
C Programming for Matrix Multiplication
Aim:
To write a C program for Matrix Multiplication
Algorithm:
Start.
1. Enter the value of m and n (or) order of the first matrix.
2. Enter the value of p and q (or) order of the second matrix.
3. Create a matrix of size a[m][n] and b[p][q].
4. Enter the element of matrices row-wise using loops.
5. If the number of columns of the first matrix is not equal to the number of rows of the
second matrix, print matrix multiplication is not possible and exit. If not, proceed to the
next step.
6. Create a third matrix, c of size m x q, to store the product.
7. Set a loop from i=0 to i=m.
8. Set an inner loop for the above loop from j=0 to j=q.
9. Initialise the value of the element (i, j) of the new matrix to 0.
10. Set an inner loop inside the above loop from k=0 to k=p.
11. Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third
matrix, c[i][j].
12. Print the third matrix.
13. Stop.
Program:
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
printf("Enter the value of N (N <= 10): ");
scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", & a[i][j]);
}
}
printf("Enter the elements of Matrix-B: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{ scanf("%d", & b[i][j]);
}
}
for (i = 0; i < n; i++)
{ for (j = 0; j < n; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
Downloaded by Archana N ([email protected])
lOMoARcPSD|45501237
lOMoAR cPSD| 45501237
}
printf("The product of the two matrices is: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{ printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
output
Enter the value of N (N <= 10): 2Enter the elements of Matrix-A: 2 2
22
Enter the elements of Matrix-B:2 2
22
Working of Pointers
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);// 22c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output
Address of c: 2686784Value of c: 22
Address of c: 2686784V
return 0;
}
Output
Enter information of students:
EX.NO:4 Read name and marks of n number of students and store them in a FILE
Aim:
To write a C program for read name and marks of n number of students and store them ina file
Algorithm:
1. char name[50];
2. int marks, i, num;
3. printf("Enter number of students: ");
4. scanf("%d", &num);
5. FILE *fptr;
6. fptr = (fopen("C:\\student.txt", "w"));
7. if(fptr == NULL)
8. printf("Error!");
9. exit(1);
10. for(i = 0; i < num; ++i)
11. printf("For student%d\nEnter name: ", i+1);
12. scanf("%s", name);
13. printf("Enter marks: ");
14. scanf("%d", &marks);
15. fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);
16. fclose(fptr);
Program:
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i = 0; i < num; ++i)
{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);
}
fclose(fptr); return 0;
}
Output
Enter number of students: 2
Step 1- Display messages like “press 1 for addition, 2 for subtraction, 3 for
multiplication,4 for division”
Step 2- accept the choice from the user and store it in a variable (say choice)
Step 3- put choice in the switch as switch(choice)
Step 4- put the case accordingly like case 1 for addition, case 2 for subtraction and so on
and store and print the result. Also give a default case for wrong choice
Program:
#include <stdio.h>
int main()
{
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);switch (op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constantdefault:
printf("Error! operator is not correct");
}
return 0;
}
Output
Enter an operator (+, -, *,):
*Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
Aim:
To Write a C program for EMI Calculator program in C
Procedure:
EMI Calculator: This program will read total loan amount (principal), rateand time in
years and prints the per month EMI of that loan amount.
The formula used in this program is:
(P*R*(1+R)T)/(((1+R)T)-1)
Here,
P is loan amount.
R is interest rate per month - we will read in yearly and convert in monthly in
program.
T is loan time period in year - we will read in yearly and convert in monthly in
program
Program:
#include <stdio.h>
#include <math.h>
int main()
{
float principal, rate, time, emi;
printf("Enter principal: ");
scanf("%f", &principal);
printf("Enter rate: ");
scanf("%f", &rate);
printf("Enter time in year: ");
scanf("%f", &time);
rate = rate / (12 * 100); /*one month interest*/time = time * 12; /*one month period*/
emi = (principal * rate * pow(1 + rate, time)) / (pow(1 + rate, time) - 1);
printf("Monthly EMI is= %f\n", emi);
return 0;
}
Output:
Enter principal: 20000
Enter rate: 10
Enter time in year: 2
Monthly EMI is= 922.899536
Aim:
Write a program to Array implementation of Stack
and Queue ADTs
Algoritham
struct Stack
{
int top;
unsigned capacity;int* array;
};
}
int peek(struct Stack* stack)
{
if (isEmpty(stack)) return INT_MIN;
return stack->array[stack->top];
}
// Driver program to test above functions
int main()
{
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
}
OUTPUT:
Algoritham :
Step 1 We will maintain two node pointer "front" and "back", which always points to the head and
tail node of linked list respectively. This will ensure that we will add node at rear of linked list and
remove node from front of linked list.
Step 2 We will start with an empty linked list, where both front and back pointer is set to NULL.
Step 3 Enqueue Operation : We will dynamically allocate memory for a struct node variable(let's
say temp). Then we will attach new node at the end of linked list by setting back->next = temp.
Finally set back pointer to temp.(back = temp;)
Step 4 Dequeue Operation : Remove head node(pointed by front pointer) of the linked list. Store the
front pointer in a temp variable. Now, move front pointer to next node(front = front->next;).
Deallocate memory of temp node using free.
Step 5 getFrontElement Operation : Returns the value of head node of linked list without removing
it.(return front->data;)
Step 6 isEmpty Check : If both front and back pointers are NULL, then Queue is empty otherwise
non-empty.
/*Program :
if(front == NULL)
printf("Queue is Empty. Unable to perform dequeue\n");
else
{
temp = front;
front = front->next;
if(front == NULL)
rear = NULL;
}
}
void printList()
{
struct node *temp = front; while(temp)
{
printf("%d->",temp->data); temp = temp->next;
}
printf("NULL\n");
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
printf("Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();
return 0;
}
Output
Queue :10->20->30->NULL
After dequeue the new Queue :20->30->
NULL After dequeue the new Queue :30->NULL
Step 2 It takes a value as an input and creates and returns a node that will have the data variableequal to the
value, and the left and right pointer variables will point to NULL.
Step 4 which takes the pointer to the parent node and the value.
Step 5 Then internally it calls the create function and the node returned by it is attached to the leftpointer of
the parent node.
Program:
// Tree traversal in C#include <stdio.h> #include <stdlib.h> struct node
{
int item;
struct node* left; struct node* right;
};
void inorderTraversal(struct node* root)
{
if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->item); inorderTraversal(root-
>right);
}
void preorderTraversal(struct node* root)
{
if (root == NULL) return; printf("%d ", root->item); preorderTraversal(root->left); preorderTraversal(root-
>right);
}
void postorderTraversal(struct node* root)
{
if (root == NULL) return; postorderTraversal(root->left); postorderTraversal(root->right);printf("%d ", root-
>item);
}
struct node* create(int value)
{
struct node* newNode = malloc(sizeof(struct node));newNode->item = value;
newNode->left = NULL; newNode->right = NULL;return newNode;
}
struct node* insertLeft(struct node* root, int value)
{
root->left = create(value);return root->left;
}
struct node* insertRight(struct node* root, int value)
{
root->right = create(value);return root->right;
}
int main()
{
struct node* root = create(1);insertLeft(root, 4);
insertRight(root, 6);
insertLeft(root->left, 42);
insertRight(root->left, 3);
insertLeft(root->right, 2);
insertRight(root->right, 33);
printf("Traversal of the inserted binary tree \n");printf("Inorder traversal \n"); inorderTraversal(root);
printf("\nPreorder traversal \n"); preorderTraversal(root); printf("\nPostorder traversal \n");
postorderTraversal(root);
}
Output :
Traversal of the inserted binary tree
Inorder traversal42 4 3 1 2 6 33
Preorder traversal1 4 42 3 6 2 33
Postorder traversal42 3 4 2 33 6 1
Step 1 Define Node class which has three attributes namely: data, left and right. Here, leftrepresents
the left child of the node and right represents the right child of the node.
Step 2 When a node is created, data will pass to the data attribute of the node and both left andright will
be set to null.
Program:
Output:
Binary search tree after insertion:10 30 50 60 70 90
Binary search tree after deleting node 90:10 30 50 60 70
Binary search tree after deleting node 30:10 50 60 70
Binary search tree after deleting node 50:10 60 70
Step 1 Searching for the key element is done in a linear fashion.Step 2 It is the simplest searching technique.
Step 3 It does not expect the list to be sorted.
Step 4 Limitation − It consumes more time and reduce the power of system.
Program:
#include<stdio.h>
int main ()
{
int a[50], n, i, key, flag = 0;
printf("enter the no: of elements");
scanf ("%d",&n);
printf("enter the elements:");
for (i=0; i<n; i++)
scanf( "%d", &a[i]);
printf("enter a key element:");
scanf ("%d", &key);
for (i=0; i<n; i++)
{
if (a[i] == key)
{
flag = 1;
break;
}
}
if (flag == 1)
printf("search is successful:");
else
printf("search is unsuccessfull:");
return 0;
}
Output
enter the no: of elements5
enter the elements:12
45
13
67
78
enter a key element:67search
is successful:
Aim:
To Write a program to implement merge sort in C language.
Algorithm:
Program:
#include <stdio.h>
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;int n2 = end - mid;
int LeftArray[n1], RightArray[n2]; //temporary arraysfor (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];
}
else
{
a[k] = RightArray[j];j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];j++;
k++;
}
}
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}
void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");printArray(a, n);
return 0;
}
Output:
Before sorting array elements are –12 31 25 8 32 17 40 42
After sorting array elements are –8 12 17 25 31 32 40 42
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.Step2 - Pick the next
element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to thenext
element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
Program:
#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++)
{
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j])
{a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
Output:
Aim:
Algorithm:
Step 2 It’s exactly same as index page of a book. In index page, every topic is associated with apage
number.
Step 3 If we want to look some topic, we can directly get the page number from the index.Step 4 Likewise, in
hashing every value will be associated with a key.
Step 5 Using this key, we can point out the element directly.
Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
struct DataItem
{
int data;int key;
};
struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key)
{
return key % SIZE;
}
struct DataItem *search(int key)
{
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL)
{
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
hashIndex %= SIZE;
}
return NULL;
}
void insert(int key,int data)
{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)
{
++hashIndex; hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item)
{
int key = item->key;get the hash
int hashIndex = hashCode(key); while(hashArray[hashIndex] != NULL)
{
if(hashArray[hashIndex]->key == key)
{
struct DataItem* temp = hashArray[hashIndex];hashArray[hashIndex] = dummyItem;
return temp;
}
++hashIndex;
hashIndex %= SIZE;
}
return NULL;
}
void display()
{
int i = 0;
for(i = 0; i<SIZE; i++)
{ if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
int main()
{
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);
if(item != NULL)
{
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
delete(item);
item = search(37);
if(item != NULL)
{
printf("Element found: %d\n", item->data);
}
else
{
printf("Element not found\n");
}
}
Output:
~~
(1,20) (2,70) (42,80) (4,25)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~(12,44) (13,78) (14,32)
~ ~ ~ ~ (17,11) (37,97)
~~