Write a C program that uses non recursive function to search for a Key value in a given list of integers using Linear search
.
Algorithm:
Step 1: Start Step 2: Read n, a[i], key values as integers Step 3: Search the list While (a[i] != key && i <= n) i = i + 1 Repeat step 3 Step 4: Successful search if(i = n + 1) then print “Element not found in the list” Return(0) else print “Element found in the list” Return (i) Step 6: Stop
Program:
#include<stdio.h> #include<conio.h> void main() { int i, a[20], n, key, flag = 0; clrscr(); printf(“Enter the size of an array \n”); scanf(“%d”, &n); printf(“Enter the array elements”); for(i = 0; i < n; i++) { scanf(“%d”, &a[i]); } printf(“Enter the key elements”); scanf(“%d”, &key); for(i = 0; i < n; i++) { if(a[i] == key) { flag = 1; break; } } if(flag == 1) printf(“The key elements is found at location %d”, i + 1); else printf(“The key element is not found in the array”); getch(); }
Input & Output:
Enter the size of an array 6
Enter the array elements 50 10 5 200 20 1
Enter the key element 1
The key Element is found at location 6
-
UpdatedOct 21, 2014
-
Views39,240
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.