Thirty random numbers between 1 and 20 and search whether it exist




Thirty random numbers between 1 and 20 are entered into an array named nums and the number to be searched is entered through the keyboard by the user. Write a C program, numSearch.c, to display the generated random numbers and find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array. The user can enter more than one number to be tested for search. The program terminates when the user enters a number that is less or equal to 0. A Sample interaction is as follow: 

#include<stdio.h>

#include<stdlib.h>

int main(){

int nums[30], i;

for(i=0; i<30; i++){

nums[i] = rand()%20 +1;

}

printf("Generated numbers are: ");

for(i=0; i<30; i++)

printf("%d, ", nums[i]);

int search;

while(1){

printf("\nEnter a number to be found (<=0 for exit): ");

scanf("%d", &search);

if(search<=0)

break;

int count = 0;

for(i=0; i<30; i++){

if(nums[i]==search)

count++;

}

printf("%d appears %d time(s)\n", search, count);

}

printf("End\n");

return 0;

}

Comments