Skip to content

Instantly share code, notes, and snippets.

@towkir
Created October 8, 2016 11:11
Show Gist options
  • Save towkir/9f2aacc007286dfb98859fcaebece397 to your computer and use it in GitHub Desktop.
Save towkir/9f2aacc007286dfb98859fcaebece397 to your computer and use it in GitHub Desktop.
This C program has an array already saved in the source and it displays it; then requests a number from user, if the input number matches with any one in that array, it returns the position of that number in the array; otherwise, says that it is not in the array;
#include <stdio.h>
int main() {
int data[11] = {10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60};
int entry, i, position=1;
printf("Here is the array:\n");
for (i=0; i<11; i++){
printf("%d\t", data[i]);
}
printf("\n");
printf("Enter your number which you want to know about:\n");
scanf("%d", &entry);
for (i=0; i<11; i++){
if (entry == data[i]){
printf("The position of your number in that array is: %d\n", position);
break;
} else {
position++;
if (position > 11) {
printf("Seems like the number you entered is not in the array\n");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment