Searching Element in an Array : C Program

Computer Science » C Program » Searching Element in an Array

Program :

#include<stdio.h>
 
int main() 
{
   int a[30], element, num, i;
 
   printf("nEnter no of elements : ");
   scanf("%d", &num);
 
   printf("nEnter the values : ");
   for (i = 0; i < num; i++) {
      scanf("%d", &a[i]);
   }
 
   //Read the element to be searched
   printf("nEnter the element to be searched : ");
   scanf("%d", &element);
 
   //Search starts from the zeroth location
   i = 0;
   while (i < num && element != a[i]) {
      i++;
   }
 
   //If i < num then Match found
   if (i < num) {
      printf("nNumber found at the location : %d", i + 1);
   } else {
      printf("nNumber not found.");
   }
 
   return (0);
}

Output 1:

Enter no of elements : 5
Enter the values : 10 20 30 40 50
Enter the element to be searched : 30
Number found at the location : 3

Output 1:

Enter no of elements : 5
Enter the values : 10 20 30 40 50
Enter the element to be searched : 60
Number not found.
Subject Name : Computer Science
Exam Name : IIT GATE, UPSC ESE, RRB, SSC, DMRC, NMRC, BSNL, DRDO, ISRO, BARC, NIELIT
Posts Name : Assistant Engineer, Management Trainee, Junior Engineer, Technical Assistant
Computer Science Books

GATE 2023 Total InfoENGG DIPLOMAUGC NET Total Info
IES 2023 Total InfoPSUs 2022 Total InfoCSIR UGC NET Total Info
JAM 2023 Total InfoM TECH 2023 Total InfoRAILWAY 2022 Total Info

Related Posts

  • Insert Element in An Array : C ProgramComputer Science » C Program » Insert Element in An Array Program : [php]#include<stdio.h> int main() { int arr[30], element, num, i, location; printf("nEnter no of elements : "); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("nEnter the element to be inserted : "); scanf("%d", &element); printf("nEnter the location : "); scanf("%d", &location); //Create space at the specified location for (i = num; i >= location; i--) { arr[i] = arr[i - 1]; } num++; arr[location - 1] = element; //Print out the result of insertion for (i = 0; i < num; i++) printf("n %d", arr[i]); return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Enter the element to be inserted : 60 Enter the location : 3 10 20 60 30 40 50[/php]
    Tags: program, element, array, computer, science, output, programs
  • Check Whether a Number is Palindrome or Not : C ProgramComputer Science » C Program » Check Whether a Number is Palindrome or Not  Program : [php]#include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("nEnter an integer : "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("n%d is a palindrome.", originalN); else printf("n%d is not a palindrome.", originalN); return 0; }[/php] Output 1: [php]Enter an integer: 101 101 is a palindrome.[/php] Output 2: [php]Enter an integer: 102 102 is not a palindrome.[/php]
    Tags: program, output, computer, science, programs
  • Binary Numbers Pyramid : C ProgramComputer Science » C Program » Binary Numbers Pyramid  Program : [php]#include<stdio.h> int main() { int i, j; int count = 1; for (i = 1; i <= 5; i++) { printf("n"); for (j = 1; j <= i; j++) { printf("%d", count % 2); count++; } if (i % 2 == 0) count = 1; else count = 0; } return(0); }[/php] Output : [php]1 01 101 0101 10101[/php]
    Tags: program, computer, science, output, programs
  • Find Average of Two Numbers : C ProgramComputer Science » C Program » Find Average of Two Numbers Program : [php]#include <stdio.h> int main() { int num1, num2; float avg; printf("nEnter first number : "); scanf("%d",&num1); printf("nEnter second number : "); scanf("%d",&num2); avg= (float)(num1+num2)/2; printf("nAverage of %d and %d is : %f",num1,num2,avg); return 0; }[/php] Output : [php]Enter first number : 5 Enter second number : 3 Average of 5 and 3 is : 4.00[/php]
    Tags: program, computer, science, output, programs
  • Check Whether a Number is Prime or Not : C ProgramComputer Science » C Program » Check Whether a Number is Prime or Not  Program : [php]#include<stdio.h> int main() { int num, i, count = 0; printf("nEnter a number :"); scanf("%d", &num); for (i = 2; i <= num / 2; i++) { if (num % i == 0) { count++; break; } } if (count == 0) printf("n%d is a prime number.", num); else printf("n%d is not a prime number.", num); return 0; }[/php] Output 1: [php]Enter a number : 5 5 is a prime number.[/php] Output 2: [php]Enter a number : 4 4 is not a prime number.[/php]
    Tags: program, output, computer, science, programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here