KEMBAR78
Array Programs in C | PDF | Integer (Computer Science) | Linear Algebra
0% found this document useful (0 votes)
7 views8 pages

Array Programs in C

The document contains multiple C programs that demonstrate basic array and matrix operations, including reading and displaying array elements, calculating the sum and average, finding maximum and minimum elements, reversing an array, counting even and odd elements, performing matrix addition, and calculating the transpose of a matrix. Each program includes sample input and output to illustrate functionality. The examples are structured to provide clear and concise coding practices for handling arrays and matrices.

Uploaded by

sathya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Array Programs in C

The document contains multiple C programs that demonstrate basic array and matrix operations, including reading and displaying array elements, calculating the sum and average, finding maximum and minimum elements, reversing an array, counting even and odd elements, performing matrix addition, and calculating the transpose of a matrix. Each program includes sample input and output to illustrate functionality. The examples are structured to provide clear and concise coding practices for handling arrays and matrices.

Uploaded by

sathya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Read and Display Array Elements

#include <stdio.h>
int main() {
int arr[5], i;
printf("Enter 5 integers: ");
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
printf("Array elements: ");
for(i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}

Output:
Enter 5 integers: 1 2 3 4 5
Array elements: 1 2 3 4 5
Find Sum and Average of Array
#include <stdio.h>
int main() {
int arr[5], i, sum = 0;
float avg;
printf("Enter 5 integers: ");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
avg = sum / 5.0;
printf("Sum = %d\n", sum);
printf("Average = %.2f", avg);
return 0;
}

Output:
Enter 5 integers: 1 2 3 4 5
Sum = 15
Average = 3.00
Find Maximum Element in Array
#include <stdio.h>
int main() {
int arr[5], i, max;
printf("Enter 5 integers: ");
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
max = arr[0];
for(i = 1; i < 5; i++) {
if(arr[i] > max)
max = arr[i];
}
printf("Maximum element = %d", max);
return 0;
}

Output:
Enter 5 integers: 5 7 2 9 1
Maximum element = 9
Find Minimum Element in Array
• #include <stdio.h>
• int main() {
• int arr[5], i, min;
• printf("Enter 5 integers: ");
• for(i = 0; i < 5; i++)
• scanf("%d", &arr[i]);
• min = arr[0];
• for(i = 1; i < 5; i++) {
• if(arr[i] < min)
• min = arr[i];
• }
• printf("Minimum element = %d", min);
• return 0;
• }

• Output:
• Enter 5 integers: 5 7 2 9 1
• Minimum element = 1
Reverse an Array
• #include <stdio.h>
• int main() {
• int arr[5], i;
• printf("Enter 5 integers: ");
• for(i = 0; i < 5; i++)
• scanf("%d", &arr[i]);
• printf("Reversed array: ");
• for(i = 4; i >= 0; i--)
• printf("%d ", arr[i]);
• return 0;
• }

• Output:
• Enter 5 integers: 1 2 3 4 5
• Reversed array: 5 4 3 2 1
Count Even and Odd Elements
• #include <stdio.h>
• int main() {
• int arr[5], i, even=0, odd=0;
• printf("Enter 5 integers: ");
• for(i = 0; i < 5; i++) {
• scanf("%d", &arr[i]);
• if(arr[i] % 2 == 0)
• even++;
• else
• odd++;
• }
• printf("Even count = %d\nOdd count = %d", even, odd);
• return 0;
• }

• Output:
• Enter 5 integers: 1 2 3 4 5
• Even count = 2
• Odd count = 3
#include <stdio.h>
int main() {
Matrix Addition
int a[2][2], b[2][2], sum[2][2], i, j;
printf("Enter elements of first matrix:\n"); Output:
for(i = 0; i < 2; i++) Enter elements of first matrix:
for(j = 0; j < 2; j++) 12
34
scanf("%d", &a[i][j]);
Enter elements of second
printf("Enter elements of second matrix:\n"); matrix:
for(i = 0; i < 2; i++) 56
for(j = 0; j < 2; j++) 78
scanf("%d", &b[i][j]); Sum of matrices:
for(i = 0; i < 2; i++) 68
10 12
for(j = 0; j < 2; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Transpose of a Matrix
#include <stdio.h>
int main() {
int a[2][2], transpose[2][2], i, j;
printf("Enter elements of matrix:\n");
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
transpose[j][i] = a[i][j];
printf("Transpose of matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++)
printf("%d ", transpose[i][j]);
printf("\n");
}
return 0;
}

Output:
Enter elements of matrix:
12
34
Transpose of matrix:
13
24

You might also like