欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言判断判断数组中是否包含某个元素

程序员文章站 2022-06-10 20:28:51
...
#include <stdio.h>
int main() {
        int nums[10] = { 1, 10, 6, 296, 177, 23, 0, 100, 34, 999 };
        int i, num, thisindex = -1;

        printf("Input an integer: ");
        scanf("%d", &num);
        for (i = 0; i<10; i++) {
                if (nums[i] == num) {
                thisindex = i;
                break;
                }
        }
        if (thisindex < 0) {
                printf("%d isn't in the array.\n", num);
        }
        else {
                printf("%d is in the array, it's index is %d.\n", num, thisindex);
        }

        return 0;
}

相关标签: C