lab.pdf
lab.pdf
elements.
Code
#include <stdio.h>
int main() {
return 0;
Output
Code
#include <stdio.h>
int main() {
int arr[6] = {10, -5, 20, -15, 30, -25};
int i;
if(arr[i] < 0) {
printf("%d\n", arr[i]); }
}
return 0;
Output
#include <stdio.h>
int main() {
int i, max;
max = arr[0];
max = arr[i];
return 0;
}
Output
Code
#include <stdio.h>
int main() {
int i, min;
min = arr[0];
min = arr[i];
return 0;
Output
int main() {
max = arr[0];
second_max = arr[0];
second_max = max;
max = arr[i];
second_max = arr[i];
return 0;
Output
Q6 Write a C program to count total number of even and odd elements in an
array.
Code
#include <stdio.h>
int main() {
if(arr[i] % 2 == 0) {
even_count++;
else {
odd_count++;
return 0;
Output
Q7 Write a C program to copy all elements from an array to another array.
Code
#include <stdio.h>
int main() {
int i;
arr2[i] = arr1[i];
return 0;
Output
#include <stdio.h>
int main() {
int pos = 2;
int i;
return 0;
Output
Code
#include <stdio.h>
int main() {
int i;
if(arr[i] == key) {
return 0;
return 0;
Output
Q10 C Program to Search an Element in an Array (Binary search)
Code
#include <stdio.h>
int main() {
if(arr[mid] == key) {
return 0;
low = mid + 1;
else {
high = mid - 1;
return 0;
}
Output
Q11 C Program to Sort an Array using Bubble Sort and Merge Sort
Code
1.Bubble Sort
#include <stdio.h>
int main() {
int i, j, temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
printf("Sorted Array:\n");
}
return 0;
2.Merge Sort
#include <stdio.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0, j = 0, k = l;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
}
while(j < n2) {
k++;
if(l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
int i;
mergeSort(arr, 0, 4);
return 0;
OutPut
1.
2.
Q12 C Program to Sort an Array Using Selection Sort and Array Using
Insertion Sort.
Code
1. Selection Sort
#include <stdio.h>
int main() {
int arr[5] = {50, 10, 40, 20, 30};
int i;
selectionSort(arr, 5);
return 0;
}
2. Insertion Sort
#include <stdio.h>
int main() {
int arr[5] = {50, 10, 40, 20, 30}; // only 5 elements
int i;
insertionSort(arr, 5);
return 0;
}
Output
1.Selection Sort
2.Insertion Sort
Code
#include <stdio.h>
int main() {
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
return 0;
}
Output
Code
#include <stdio.h>
int main() {
int merged[6];
int i;
merged[i] = arr1[i];
merged[i + 3] = arr2[i];
// Merged array
printf("Merged Array:\n");
}
return 0;
Output
Code
#include <stdio.h>
int main() {
int i, count = 0;
if (arr[i] == search) {
count++;
return 0;
Output
Q16 C Program to Find the Transpose of a matrix and Trace of a matrix
Code
#include <stdio.h>
int main() {
{4, 5, 6},
{7, 8, 9} };
int i, j, trace = 0;
transpose[j][i] = matrix[i][j];
trace += matrix[i][i];
// Matrix print
printf("Original Matrix:\n");
printf("\n");
// Transpose print
printf("\nTranspose of Matrix:\n");
printf("\n");
// Trace print
return 0;
Output
Q17 C Program to check if two matrices are equals or not
Code
#include <stdio.h>
int main() {
if (matrix1[i][j] != matrix2[i][j]) {
flag = 0;
break;
// Result print
if (flag == 1)
else
return 0;
Output
Q18C program to check symmetric matrix
Code
#include <stdio.h>
int main() {
{2, 1} };
int i, j, flag = 1;
if (matrix[i][j] != matrix[j][i]) {
flag = 0;
break;
// Result print
if (flag == 1)
printf("Matrix is Symmetric.\n");
else
OutPut
Code
#include <stdio.h>
int main() {
{7, 8} };
int sum[2][2];
int i, j;
}
}
// Result print
printf("Sum of the Matrices:\n");
printf("\n");
return 0;
Output
Code
#include <stdio.h>
int main() {
{3, 4} };
{7, 8} };
int i, j, k;
// Multiplication logic
for (i = 0; i < 2; i++) {
result[i][j] = 0;
// Result print
printf("\n");
return 0;
Output
Q21C program to find the length of a string without using library function
Code
#include <stdio.h>
int main() {
int i = 0, length = 0;
length++;
i++;
// Result print
return 0;
Output
Code
#include <stdio.h>
int main() {
int i = 0, j = 0;
i++;
str1[i] = str2[j];
i++;
j++;
str1[i] = '\0';
// Result print
return 0;
OutPut
#include <stdio.h>
int main() {
int i = 0, count = 0;
// Taking input
scanf("%c", &ch);
if (str[i] == ch) {
count++;
i++;
// Result print
return 0;
Output
Q24 C Program to check if the string is palindrome or not
Code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
length = strlen(str);
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
length--;
break;
}
}
// Result print
if (isPalindrome)
else
return 0;
Output
Q25 C Program to Reverse a String with and without using library function
Code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
str[strcspn(str, "\n")] = 0;
strrev(str);
return 0;
#include <stdio.h>
#include <string.h>
int main() {
int i, length;
length = strlen(str);
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
// Reverse manually
rev[length] = '\0';
return 0;
Output
Q26C Program to count the number of vowels present in the sentence
Code
#include <stdio.h>
int main() {
char str[200];
int i = 0, count = 0;
count++;
i++;
// Result print
return 0;
}
Output
Code
#include <stdio.h>
#include <string.h>
int main() {
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
// String compare
if (result == 0)
return 0;
Output
Code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
str[strcspn(str, "\n")] = 0;
strupr(str);
return 0;
}
Output
Code
#include <stdio.h>
int main() {
j++;
wordLength++;
} else {
int k;
longestWord[k] = tempWord[k];
longestWord[k] = '\0';
wordLength = 0;
i++;
return 0;
Output
Q30C program to arrange names in alphabetical order
Code
#include <stdio.h>
#include <string.h>
int main() {
int n, i, j;
scanf("%d", &n);
getchar();
// Names input
// **Bubble Sort**
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
// **Sorted names print**
printf("%s\n", names[i]);
return 0;
Output
Q31 C program to find the arithmetic mean of a given list of n real values
using pointers
Code
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
scanf("%f", &numbers[i]);
ptr = numbers;
// Mean calculate
mean = sum / n;
// Output mean
return 0;
Output
Q32 C program to Declare a Two-Dimensional Array of Pointers
Code
#include <stdio.h>
int main() {
printf("\n");
return 0;
Output
Code
#include <stdio.h>
int main() {
scanf("%d", &n);
ptr = arr;
max = *ptr;
return 0;
Output
Code
#include <stdio.h>
int main() {
int a, b, temp; int *p1, *p2;
= &b;
temp = *p1;
*p1 = *p2;
*p2 = temp;
return 0;
Output
Code
#include <stdio.h>
int main() {
int a, b, sum;
// User input
printf("Enter two numbers: ");
p1 = &a;
p2 = &b;
// Sum print
return 0;
Output
Code
#include <stdio.h>
int main() {
int n, i, j, temp;
scanf("%d", &n);
scanf("%d", &arr[i]);
p = arr;
*(p + j + 1) = temp;
return 0;
Output
Q37 C Program to Store Information of a Students Using Structure
Code
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
scanf("%s", s.name);
scanf("%d", &s.roll);
scanf("%f", &s.marks);
// Student info print
printf("\nStudent Information:\n");
return 0;
Output
Code
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
int i;
scanf("%s", s[i].name);
scanf("%d", &s[i].roll);
scanf("%f", &s[i].marks);
printf("\nStudent Information:\n");
return 0;
Output
1.Entering Data
2.Storing Data
Q39 c program to read details of n students and print the list of students
who have scored 75 marks and above
Code
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
char name[n][50];
// Input details
scanf("%s", name[i]);
scanf("%d", &roll[i]);
scanf("%d", &marks[i]);
return 0;
Output