samedi 21 novembre 2020

I want to print values in a 2D array depending on what the last digit of that value is

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m = 0, n = 0, key = 0;
    int array[m][n];
    printf("Enter the number of rows in the array\n");
    scanf("%d", &m);
    printf("Enter the number of columns in the array\n");
    scanf("%d", &n);

    printf("Enter the numbers in the array:\n");
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &array[i][j]);
        }
    }

    printf("Display the values that end with number: ");
    scanf("%d", &key);

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
           if ((array[i][j] % 10) == key)
           {
               printf("%d\n", array[i][j]);
           }
        }
    }

    return 0;
}

I have set the for loops and the if statement but the program is ignoring the condition. What I expect is for the program to print all the values ending in a specific number. For example, if the input is 36,26,25,24, and the key is 6, the program should print the number 36 and 26. I have looked at the code but I cannot seem to figure out the problem.

Aucun commentaire:

Enregistrer un commentaire