Saturday 30 September 2017

Search an element in an array

#include<stdio.h>
void main() {
   int a[30], ch, 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 elements to be searched :");
   scanf("%d", &ch);
   //Search starts from the zeroth location
   i = 0;
   while (i < num && ch != a[i]) {
      i++;
   }
   //If i < num then Match found
   if (i < num) {
      printf("Number found at the location = %d", i + 1);
   } else {
      printf("Number not found");
   }

No comments:

Post a Comment

Convert a square matrix to Lower and Upper Triangular Matrix

Upper and Lower triangle Matrix Using C program In this program we will be discussing on how to convert a square matrix into correspon...