This is a C program to print the kth element in the array.
This C Program implements an array prints the kth element in the array where the position k is entered by the user as an input.
1. Create an array and taking its size from the users, define all its elements.
2. Take an input from users, the position in the array where we want to access element.
3. The element would be in the index (entered_position -1) of the array, print it.
Here is source code of the C Program to print the kth element in the array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Print the kth Element in the Array
*/
#include <stdio.h>
int main()
{
int arr[100], len, i, j, temp, n;
printf("Enter the size of array");
scanf("%d", &len);
printf("\n Enter the array elements");
for (i = 0; i < len; i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter Which kth Number You want");
scanf("%d", &n);
printf("\n The %d th kth number is: %d", n, arr[n - 1]);
return 0;
}
1. Declare an array of some fixed capacity, 100.
2. Take size from users as an input.
3. Using for loop, define the elements of the array according to the size.
4. Enter the position(i.e k) where we want to access the element.
5. The index of the array where we will find the required element according to the position entered is – (k-1).
Enter the size of array4 Enter the array elements 12 13 17 20 Enter Which kth Number You want 4 The 4th kth number is: 20
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C Books
- Apply for C Internship
- Check Computer Science Books