Write a C program to print the elements at the odd and even positions in an array.
#include <stdio.h>
#define MAXLIMIT 256
int main() {
int arr[MAXLIMIT], i, n;
/* get the number of elements from the user */
printf("Enter the number of elements:");
scanf("%d", &n);
/* boundary check */
if (n > MAXLIMIT || n < 0) {
printf("Bourndary Level Exceeded!!\n");
return 0;
}
/* get array elements from the user */
printf("Enter your Array Inputs:\n");
for (i = 0; i < n; i++) {
printf("Data[%d]: ", i);
scanf("%d", &arr[i]);
}
/* printing the elements at the even position */
printf("Elements at the even position:\n");
for (i = 0; i < n; i++) {
if (i % 2 == 0)
printf("%d ", arr[i]);
}
/* printing the elements at the odd position */
printf("\n\nElements at the odd position:\n");
for (i = 0; i < n; i++) {
if (i % 2 != 0)
printf("%d ", arr[i]);
}
#define MAXLIMIT 256
int main() {
int arr[MAXLIMIT], i, n;
/* get the number of elements from the user */
printf("Enter the number of elements:");
scanf("%d", &n);
/* boundary check */
if (n > MAXLIMIT || n < 0) {
printf("Bourndary Level Exceeded!!\n");
return 0;
}
/* get array elements from the user */
printf("Enter your Array Inputs:\n");
for (i = 0; i < n; i++) {
printf("Data[%d]: ", i);
scanf("%d", &arr[i]);
}
/* printing the elements at the even position */
printf("Elements at the even position:\n");
for (i = 0; i < n; i++) {
if (i % 2 == 0)
printf("%d ", arr[i]);
}
/* printing the elements at the odd position */
printf("\n\nElements at the odd position:\n");
for (i = 0; i < n; i++) {
if (i % 2 != 0)
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of elements:5
Enter your Array Inputs:
Data[0]: 1
Data[1]: 2
Data[2]: 3
Data[3]: 4
Data[4]: 5
Elements at the even position:
1 3 5
Elements at the odd position:
2 4
Enter the number of elements:5
Enter your Array Inputs:
Data[0]: 1
Data[1]: 2
Data[2]: 3
Data[3]: 4
Data[4]: 5
Elements at the even position:
1 3 5
Elements at the odd position:
2 4
No comments:
Post a Comment