1.

Write a C++ program to find the position of a given number in an array.

Answer»

#include <iostream>

void main()

{

int a[100],n,i,position = -1,ele;

cout<<"Enter the size";

cin>>n;

cout <<"Enter the values";

for (i=0;i<n;i++)

cin>>a[i];

cout<<"Now,Enter the number to find its position";

cin>> ele;

for(i=0;i<n;i++)

If(a[i] = = else)

Position = i;

if(position>=0)

cout<<"The position of the number is "<<position<<end1;

else

cout<<"The number is not found"<<end1;

Here is the source code of the C++ Program to Print the Element at a Given Position.

 #include<iostream>

using namespace std;

int main ()

{

    char arr[10], ele;

    int i, n, pos;

    cout << "Enter size of the array : ";

    cin >> n;

    cout << "\nEnter elements of the array : ";

    for (i = 1; i <= n; i++)

        cin >> arr[i];

    cout << "\nEnter the position : ";

    cin >> pos;

    for (i = 1; i <= n; i++)

        if (pos == i)

            ele = arr[i];

    cout << "\nElement at position " << pos << " is : " << ele;

    return 0;

}



Discussion

No Comment Found

Related InterviewSolutions