198k views
0 votes
Write a program, called array_index.c, that asks the user to enter 10 positive integers, stores them in an array of size 10, then asks the user for another number k, which is between 1 and 10, and prints out the kth number that the user has entered.

1 Answer

3 votes

Answer:

#include <stdio.h>

int main()

{

int numbers[10];

int k;

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

printf("Enter a number: ");

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

}

printf("Which number you want to print[1-10]? ");

scanf("%d", &k);

printf("The %d. number is %d", k, numbers[k-1]);

return 0;

}

Step-by-step explanation:

Declare an array named numbers that has a size of ten and an integer named k

Create a for loop that iterates 10 times. Inside the loop, ask the user to enter the numbers for the array and put them in the array

When the loop is done, ask the user to enter which number to print and print the number

Note that, since index starts at 0, the 5. element is at index 4. That is why we need to subtract 1 from k when specifying the index.

User Amardeepvijay
by
4.6k points