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

Array Programs in C

The document contains multiple C programming examples demonstrating basic array operations such as reading and displaying elements, calculating sum and average, finding maximum and minimum elements, reversing an array, counting even and odd elements, and performing matrix addition and transposition. Each example includes the code and sample output for user input. These examples serve as practical applications of array manipulation in C programming.

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)
6 views8 pages

Array Programs in C

The document contains multiple C programming examples demonstrating basic array operations such as reading and displaying elements, calculating sum and average, finding maximum and minimum elements, reversing an array, counting even and odd elements, and performing matrix addition and transposition. Each example includes the code and sample output for user input. These examples serve as practical applications of array manipulation in C programming.

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

• Code:
• #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
• Code:
• #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
• Code:
• #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
• Code:
• #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
• Code:
• #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
• Code:
• #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
Matrix Addition
• Code:
• #include <stdio.h>
• int main() {
• int a[2][2], b[2][2], sum[2][2], i, j;
• printf("Enter elements of first matrix:\n");
• for(i = 0; i < 2; i++)
• for(j = 0; j < 2; j++)
• scanf("%d", &a[i][j]);
• printf("Enter elements of second matrix:\n");
• for(i = 0; i < 2; i++)
• for(j = 0; j < 2; j++)
• scanf("%d", &b[i][j]);
• for(i = 0; i < 2; i++)
• 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
• Code:
• #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

You might also like