Search a specified integer in an array

Problem statement:- Program to Search a specified integer in an array

Data requirement:-

   Input Data:- arr,size, ele

  Output Data:-arr

  Additional Data:- i, j, temp

Program in C

Here is the source code of the C Program to Search a specified integer in an array.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,temp=0;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int ele;
    printf("\nEnter the search element:");
    scanf("%d",&ele);
    printf("\nArray elements are:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    for(i=0;i<size;i++)
    {
        if(arr[i]==ele)
            temp=1;
    }
    if(temp==1)
        printf("\nElement found....");
    else
        printf("\nElement not found....");
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
3
7
10
12

Enter the search element:12

Array elements are:4 3 7 10 12
Element found....

Program in C++

Here is the source code of the C++ Program to Search a specified integer in an array.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }

    int ele;
    cout<<"\nEnter the search element:";
    cin>>ele;
    cout<<"\nArray elements are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    for(i=0;i<size;i++)
    {
        if(arr[i]==ele)
            temp=1;
    }
    if(temp==1)
        cout<<"\nElement found....";
    else
        cout<<"\nElement not found....";
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
3
100
125
300

Enter the search element:5

Array elements are:3 100 125 300
Element not found....

Program in Java

Here is the source code of the Java Program to Search a specified integer in an array.

Code:

import java.util.Scanner;
public class p23 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int i,temp=0,size;
    size=sc.nextInt();
    int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
     for(i=0;i<size;i++)
    {
        arr[i]=sc.nextInt();
    }
     int ele;
     System.out.print("\nEnter the search element:");
     ele=sc.nextInt();
     
     System.out.print("\nArray elements are:");
     for(i=0;i<size;i++)
     {
    System.out.print(arr[i]+" ");
     }
     for(i=0;i<size;i++)
     {
         if(arr[i]==ele)
             temp=1;
     }
     if(temp==1)
    System.out.print("\nElement found....");
     else
    System.out.print("\nElement not found....");
     
           sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
12
3000
50
20
447

Enter the search element:20

Array elements are:12 3000 50 20 447 
Element found....

Program in Python

Here is the source code of the Python Program to Search a specified integer in an array.

Code:

arr=[]
temp=0
pos=0
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
print("Enter the search element:")
ele=int(input())
print("Array elements are:")
for i in range(0,size):
    print(arr[i],end=" ")
for i in range(0,size):
    if arr[i] == ele:
            temp = 1
if temp==1:
    print("\nElement found....")
else:
    print("\nElement not found....")

Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
20
30
40
50
Enter the search element:
50
Array elements are:
20 30 40 50 
Element found....

Post a Comment

0 Comments