0% found this document useful (0 votes)
15 views4 pages

Solutions - Arrays in C Programming Langdwwwwwuage Exercises

The document contains three exercises for C programming related to arrays. Exercise 1 involves writing a program to find the maximum and minimum values in an array along with their indexes. Exercise 2 counts even and odd numbers in a user-input array, while Exercise 3 reverses the elements of an array and displays them.

Uploaded by

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

Solutions - Arrays in C Programming Langdwwwwwuage Exercises

The document contains three exercises for C programming related to arrays. Exercise 1 involves writing a program to find the maximum and minimum values in an array along with their indexes. Exercise 2 counts even and odd numbers in a user-input array, while Exercise 3 reverses the elements of an array and displays them.

Uploaded by

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

Arrays in C Programming Language

Exercise 1 – Maximum and Minimum Finder

Question:
Write a C program that reads an array of integers from the user and finds both
the maximum and minimum values along with their indexes in the array.
Your program will ask the user to input the size of the array and the elements of the array.
The program should then determine and print:
• The largest element in the array and its index
• The smallest element in the array and its index

Sample Output:

Enter size of the array: 5

Enter element [1]=23


Enter element [2]=12
Enter element [3]=87
Enter element [4]=45
Enter element [5]=9

Maximum value is 87 at index 2


Minimum value is 9 at index 4

#include <stdio.h>

int main() {
int size, i;
int arr[100];
int max, min, maxIndex = 0, minIndex = 0;

printf("Enter size of the array: ");


scanf("%d", &size);

for(i = 0; i < size; i++) {


printf("Enter element [%d]= ", i + 1);
scanf("%d", &arr[i]);
}

max = min = arr[0]; // Initialize with first element


for(i = 1; i < size; i++) {
if(arr[i] > max) {
max = arr[i];
maxIndex = i;
}
if(arr[i] < min) {
min = arr[i];
minIndex = i;
}
}

printf("\nMaximum value is %d at index %d\n", max, maxIndex);


printf("Minimum value is %d at index %d\n", min, minIndex);

return 0;
}

Exercise 2 – Even and Odd Counting

Question:
Write a C program that reads an array from the user, then counts and displays the number
of even and odd numbers in the array.

Your program will ask the user to enter 10 values into the array.

The program should then:

• Count how many of the entered numbers are even


• Count how many are odd
• Display both counts

Sample Output:

Enter 10 values of elements:


A[1] = 5
A[2] = 8
A[3] = 12
A[4] = 7
A[5] = 19
A[6] = 6
A[7] = 33
A[8] = 14
A[9] = 21
A[10] = 4

Even numbers count = 5


Odd numbers count = 5

#include <stdio.h>

int main() {
int arr[10];
int i, even = 0, odd = 0;

printf("Enter 10 values of elements:\n");


for(i = 0; i < 10; i++) {
printf("A[%d] = ", i + 1);
scanf("%d", &arr[i]);

if(arr[i] % 2 == 0)
even++;
else
odd++;
}

printf("\nEven numbers count = %d\n", even);


printf("Odd numbers count = %d\n", odd);

return 0;
}

Exercise 3 – Reverse array

Question:
Write a C program that takes an array from the user and prints its elements in reverse
order..

Your program will ask the user to enter the size of the array.

The program should:

• Read the array elements from the user


• Display the array elements in reverse order

Sample Output:
Enter size of the array: 4
Enter element [1]=10
Enter element [2]=20
Enter element [3]=30
Enter element [4]=40

Array in reverse order: 40 30 20 10

#include <stdio.h>

int main() {
int arr[100], i, size;

printf("Enter size of the array: ");


scanf("%d", &size);

for(i = 0; i < size; i++) {


printf("Enter element [%d]= ", i + 1);
scanf("%d", &arr[i]);
}

printf("\nArray in reverse order: ");


for(i = size - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}

return 0;
}

You might also like