-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessingarray.c
More file actions
48 lines (39 loc) · 1.26 KB
/
accessingarray.c
File metadata and controls
48 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
int main() {
int size;
// Input array size
printf("Enter array size: ");
if (scanf("%d", &size) != 1 || size <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1; // Exit with an error code
}
// Declare the array and variable to store the number to be searched
int arr[size], num;
// Input array elements
printf("Enter array elements: ");
for (int i = 0; i < size; i++) {
if (scanf("%d", &arr[i]) != 1) {
printf("Invalid input. Please enter integers only.\n");
return 1; // Exit with an error code
}
}
// Input number to search
printf("Enter number to be searched: ");
if (scanf("%d", &num) != 1) {
printf("Invalid input. Please enter an integer.\n");
return 1; // Exit with an error code
}
// Search for the number in the array
int found = 0;
for (int i = 0; i < size; i++) {
if (num == arr[i]) {
printf("arr[%d] = %d\n", i, arr[i]);
found = 1; // Indicate that the number has been found
}
}
// If the number was not found, print a message
if (!found) {
printf("Number %d not found in the array.\n", num);
}
return 0;
}