Using - C
#include<stdio.h>
void main() {
int size, element, i;
printf("Enter the size of the Array: ");
scanf("%d",&size);
int arr[size], pos[size], posIndex=0;
printf("Enter %d Elements: \n");
for(i=0; i < size; i++) {
scanf("%d", &arr[i]);
}
system("cls");
// Displaying Array
printf("Given Array: ");
for(i=0; i < size; i++) {
printf("%d ",arr[i]);
}
printf("\n\nEnter the element to be searched: ");
scanf("%d",&element);
for(i=0; i < size; i++) {
if(arr[i] == element) {
pos[posIndex++] = i+1;
}
}
if(posIndex != 0) {
printf("%d found at position: ",element);
for(i=0; i < posIndex; i++) {
printf("%d, ",pos[i]);
}
return;
}
else {
printf("%d not found in this Array!",element);
return;
}
}
OUTPUT
Enter the size of the Array: 6
Enter 6 Elements:
45
89
56
45
12
45
Given Array: 45 89 56 45 12 45
Enter the element to be searched: 45
45 found at position: 1, 4, 6,
Using - C++
#include<iostream>
using namespace std;
int main() {
int size, element;
cout<< "Enter the size of the Array: ";
cin>> size;
int arr[size] , pos[size] , posIndex=0;
cout<< "Enter "<< size << " Elements: "<< endl;
for(int i=0; i < size; i++) {
cin>> arr[i];
}
system("cls");
//Displaying Array
cout<< "Given Array: ";
for(int i=0; i < size; i++) {
cout<< arr[i]<< " ";
}
cout<< "\n\nEnter the element to be searched: ";
cin>> element;
for(int i=0; i < size; i++) {
if(arr[i] == element) {
pos[posIndex++] = i+1;
}
}
if(posIndex != 0) {
cout<< element<< " found at position: ";
for(int i=0; i < posIndex; i++) {
cout<< pos[i]<< ", ";
}
return 1;
}
else {
cout<< element<< " not found in this Array!"<< endl;
return 0;
}
}
OUTPUT
Enter the size of the Array: 8
Enter 8 Elements:
12
45
78
12
56
98
12
55
Given Array: 12 45 78 12 56 98 12 55
Enter the element to be searched: 12
12 found at position: 1, 4, 7,