by CodeChum Admin
Make a program that will accept an integer and loop for the same number of times as that of the inputted integer and input random integers and add it to the array/list one by one, per line. Afterwards, make your program accept another random integer.
Using that final integer, compare from your array/list if the final integer's value is also present in your current array/list. If so, print "Present"; otherwise, print "None".
Start coding now!
Input
1. Size of the array
2. Elements of the array
3. Integer to be searched
Output
The first line will contain a message prompt to input the size of the array.
The succeeding lines will contain message prompts to input the elements of the array.
The next line will contain a message prompt to input the integer to be searched.
The last line contains the appropriate message.
Enter·the·size:·5
Element·#1:·3
Element·#2:·21
Element·#3:·2
Element·#4:·5
Element·#5:·23
Enter·the·integer·to·be·searched:·2
Present
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int i, size, n;
bool found = false;
printf("Enter the size: ");
scanf("%d", &size);
arr = (int*)malloc(size * sizeof(int));
for (i = 0; i<size; i++)
{
printf("Element #%d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Enter the integer to be searched:");
scanf("%d", &n);
for (i = 0; i < size; i++)
{
if (arr[i] == n)
found = true;
}
if (found)
printf("Present\n");
else
printf("None\n");
free(arr);
return 0;
}
Comments
Leave a comment