How to write a C Program to check if a number is in an array[1000] in C Programming Language ?
This C Program to Check if a number is in an array[1000].
Insert number for check.
If Number is found in the array[1000].
Else Number is not found in the array[1000].
Solution:
- #include <stdio.h>
- int check_number(double ar[1000], double num);
- int main()
- {
- double ar[1000], num;
- int n ,i;
- printf("n = ");
- scanf("%d", &n);
- printf("Insert Numbers : \n");
- for(i = 0 ; i < n ; i ++)
- {
- scanf("%lf", &ar[i]);
- }
- printf("Insert number for check : ");
- scanf("%lf",&num);
- if(1 == check_number(ar,num))
- {
- printf("Number is found in the array.\n");
- }
- else
- {
- printf("Number is not found in the array.\n");
- }
- return 0;
- }
- int check_number(double ar[1000], double num)
- {
- int i;
- for(i = 0 ; ar[i] != '\0' ; i++)
- {
- if(num == ar[i])
- {
- return 1;
- }
- }
- return 0;
- }