54.1k views
0 votes
Given a declaration for an array int A [ 10 ], write c-code for following cases; a. Write a loop which allows you to enter 10 values in to the array from the keyboard b. Write a loop to sum all elements in the array with odd index. c. Write a loop to print all elements in reverse order.

User Cgijbels
by
4.8k points

1 Answer

3 votes

Answer:

#include <stdio.h>

int main()

{

int A[10];

int sum = 0;

printf("Enter array elements\\");

for (int i = 0; i < 10 ; i++)

scanf("%d", &A[i]);

for (int i = 0; i < 10 ; i++) {

if (i % 2 ==1)

sum += A[i];

}

printf("Sum of odd indexed elements: %d \\", sum);

printf("Array in reverse order: ");

for (int i = 9; i >= 0; i--)

printf("%d ", A[i]);

return 0;

}

Step-by-step explanation:

Declare the array and sum variable

Ask user for the array elements and put them in the array using for loop

Calculate the sum of the numbers that are at odd index, check if the index is odd using modulo operator

Print the array in reverse order using for loop, start the index from 9 and go until 0, so that it will print from back

User Hollister
by
5.0k points