C++ Program to Print the Element at a Given Position

This is a C++ Program to Print the Element at a Given Position.

Problem Description

The program takes an array of elements and prints the element at a given position.

Problem Solution

1. The program takes an array of elements and stores them in an array.
2. The position whose element is to be printed is taken.
3. Using a for loop, the element at that position is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Print the Element at a Given Position. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     char arr[10], ele;
  6.     int i, n, pos;
  7.     cout << "Enter size of the array : ";
  8.     cin >> n;
  9.     cout << "\nEnter elements of the array : ";
  10.     for (i = 1; i <= n; i++)
  11.         cin >> arr[i];
  12.     cout << "\nEnter the position : ";
  13.     cin >> pos;
  14.     for (i = 1; i <= n; i++)
  15.         if (pos == i)
  16.             ele = arr[i];
  17.     cout << "\nElement at position " << pos << " is : " << ele;
  18.     return 0;
  19. }
Program Explanation

1. The user is asked to enter the size of the array and the value is stored in ‘n’.
2. Elements of the array are asked to enter and stored in character variable ‘arr’.
3. The position is entered and stored in the variable ‘pos’.
4. Using a for loop and initializing ‘i’ as 1, the array is traversed.
5. If pos is equal to i, then element at that position is stored in the variable ‘ele’.
6. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter size of the array : 3
Enter elements of the array : T O P
Enter the position : 2
Element at position 2 is : O
 
Case 2 :
Enter size of the array : 5
Enter elements of the array : 8 4 5 2 9
Enter the position : 1
Element at position 1 is : 8
 
Case 3 :
Enter size of the array : 4
Enter elements of the array : SWIM
Enter the position : 3
Element at position 3 is : I

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.