Lab 4 Arrays + Strings
1. Sum of Array Elements
int main()
{
int arr[100], n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0; i < n; i++)
sum += arr[i]; // adding each element
printf("Sum = %d", sum);
return 0;
}
2. Find Max and Min in Array
int main()
{
int arr[100], n, i, max, min;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
max = min = arr[0]; // assume first element
for(i = 1; i < n; i++)
{
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
}
printf("Max = %d\nMin = %d", max, min);
return 0;
}
3. Reverse an Array
int main()
{
int arr[100], n, i, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0; i < n/2; i++)
{
// swap first with last
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
printf("Reversed array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
4. Linear Search
int main()
{
int arr[100], n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++)
{
if(arr[i] == key)
{
found = 1; // element found
break;
}
}
if(found)
printf("Element found at position %d", i+1);
else
printf("Element not found");
return 0;
}
5. Bubble Sort
int main()
{
int arr[100], n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
// Bubble sort logic
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
// swap adjacent elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
6. Second Largest Element
int main()
{
int arr[100], n, i;
int max, second;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
max = second = -9999; // initial small values
for(i = 0; i < n; i++)
{
if(arr[i] > max)
{
second = max; // update second
max = arr[i];
}
else if(arr[i] > second && arr[i] != max)
{
second = arr[i];
}
}
printf("Second largest = %d", second);
return 0;
}
7. Count Even and Odd Numbers
int main()
{
int arr[100], n, i, even = 0, odd = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0; i < n; i++)
{
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even = %d\nOdd = %d", even, odd);
return 0;
}
8. Merge Two Arrays
int main()
{
int arr1[50], arr2[50], arr3[100];
int n1, n2, i, j;
printf("Enter size of first array: ");
scanf("%d", &n1);
for(i = 0; i < n1; i++)
scanf("%d", &arr1[i]);
printf("Enter size of second array: ");
scanf("%d", &n2);
for(i = 0; i < n2; i++)
scanf("%d", &arr2[i]);
// copy first array
for(i = 0; i < n1; i++)
arr3[i] = arr1[i];
// append second array
for(i = 0, j = n1; i < n2; i++, j++)
arr3[j] = arr2[i];
printf("Merged array:\n");
for(i = 0; i < n1 + n2; i++)
printf("%d ", arr3[i]);
return 0;
}
Lab 4 – 2D Arrays Programs
9. Matrix Addition
int main()
{
int a[10][10], b[10][10], sum[10][10];
int i, j, r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter first matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &b[i][j]);
// addition logic
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Result matrix:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
10. Matrix Subtraction
int main()
{
int a[10][10], b[10][10], sub[10][10];
int i, j, r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter first matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &b[i][j]);
// subtraction logic
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
sub[i][j] = a[i][j] - b[i][j];
printf("Result matrix:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
printf("%d ", sub[i][j]);
printf("\n");
}
return 0;
}
11. Matrix Multiplication
int main()
{
int a[10][10], b[10][10], res[10][10];
int i, j, k, r1, c1, r2, c2;
printf("Enter rows and cols of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and cols of second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2)
{
printf("Multiplication not possible");
return 0;
}
printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
// initialize result matrix
for(i = 0; i < r1; i++)
for(j = 0; j < c2; j++)
res[i][j] = 0;
// multiplication logic
for(i = 0; i < r1; i++)
{
for(j = 0; j < c2; j++)
{
for(k = 0; k < c1; k++)
{
// row × column multiplication
res[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Result matrix:\n");
for(i = 0; i < r1; i++)
{
for(j = 0; j < c2; j++)
printf("%d ", res[i][j]);
printf("\n");
}
return 0;
}
12. Transpose of Matrix
int main()
{
int a[10][10], trans[10][10];
int i, j, r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
// transpose logic (swap rows & columns)
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
trans[j][i] = a[i][j];
printf("Transpose matrix:\n");
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
printf("%d ", trans[i][j]);
printf("\n");
}
return 0;
}
13. Check Symmetric Matrix
int main()
{
int a[10][10], i, j, n, flag = 1;
printf("Enter size of square matrix: ");
scanf("%d", &n);
printf("Enter matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
scanf("%d", &a[i][j]);
// symmetric condition: A[i][j] == A[j][i]
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(a[i][j] != a[j][i])
{
flag = 0;
break;
}
}
}
if(flag)
printf("Matrix is Symmetric");
else
printf("Matrix is Not Symmetric");
return 0;
}
Lab 4 – Strings Programs
14. String Length (Without strlen)
int main()
{
char str[100];
int i = 0, length = 0;
printf("Enter a string: ");
gets(str); // Turbo C style input
while(str[i] != '\0') // loop until null character
{
length++;
i++;
}
printf("Length = %d", length);
return 0;
}
15. Reverse String
int main()
{
char str[100], rev[100];
int i = 0, len = 0, j;
printf("Enter a string: ");
gets(str);
// find length
while(str[i] != '\0')
{
len++;
i++;
}
// reverse logic
for(i = len - 1, j = 0; i >= 0; i--, j++)
{
rev[j] = str[i];
}
rev[j] = '\0'; // terminate string
printf("Reversed string = %s", rev);
return 0;
}
16. Palindrome String
int main()
{
char str[100];
int i = 0, len = 0, flag = 1;
printf("Enter a string: ");
gets(str);
// find length
while(str[len] != '\0')
len++;
// compare characters from start and end
for(i = 0; i < len/2; i++)
{
if(str[i] != str[len - i - 1])
{
flag = 0;
break;
}
}
if(flag)
printf("Palindrome string");
else
printf("Not a palindrome");
return 0;
}
17. Count Vowels and Consonants
int main()
{
char str[100];
int i = 0, vowels = 0, consonants = 0;
printf("Enter a string: ");
gets(str);
while(str[i] != '\0')
{
// check alphabet only
if((str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= 'A' && str[i] <= 'Z'))
{
// vowel check
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||
str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
vowels++;
else
consonants++;
}
i++;
}
printf("Vowels = %d\nConsonants = %d", vowels, consonants);
return 0;
}
18. Compare Two Strings (Without strcmp)
int main()
{
char str1[100], str2[100];
int i = 0, flag = 1;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
while(str1[i] != '\0' || str2[i] != '\0')
{
if(str1[i] != str2[i])
{
flag = 0;
break;
}
i++;
}
if(flag)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}