Practical File - C Language
Ayush Kumar (AIML 1) 28235
Aim:
Write a program in C to create an array and perform insertion and deletion operations. The program
should print the student details as the first line of output.
Apparatus Required:
1. Computer system with GCC compiler / Turbo C
2. C language software (Code::Blocks / Dev C++ / Turbo C / Online IDE)
3. Pen, Practical File
Program Code:
#include <stdio.h>
int main() {
int arr[100], n, i, pos, value, choice;
/* Print student details as first line of output */
printf("Ayush Kumar (AIML 1) 28235\n");
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements are: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("\nChoose operation:\n1. Insertion\n2. Deletion\n");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter position to insert (1 to %d): ", n+1);
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
for (i = n; i >= pos; i--) {
arr[i] = arr[i-1];
}
arr[pos-1] = value;
n++;
printf("Array after insertion: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
else if (choice == 2) {
printf("Enter position to delete (1 to %d): ", n);
scanf("%d", &pos);
for (i = pos-1; i < n-1; i++) {
arr[i] = arr[i+1];
}
n--;
printf("Array after deletion: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
else {
printf("Invalid choice!\n");
}
return 0;
}
Sample Output (Insertion example):
Ayush Kumar (AIML 1) 28235
Enter number of elements in array: 5
Enter 5 elements:
12345
Array elements are: 1 2 3 4 5
Choose operation:
1. Insertion
2. Deletion
1
Enter position to insert (1 to 6): 3
Enter value to insert: 99
Array after insertion: 1 2 99 3 4 5
Sample Output (Deletion example):
Ayush Kumar (AIML 1) 28235
Enter number of elements in array: 5
Enter 5 elements:
10 20 30 40 50
Array elements are: 10 20 30 40 50
Choose operation:
1. Insertion
2. Deletion
2
Enter position to delete (1 to 5): 2
Array after deletion: 10 30 40 50
Flowchart: