Search for the largest even digit in a given integer

I take the online class C, but the professor refuses to answer emails, and I needed help.

In any case, our task was to write a program that takes an integer from the user and finds the largest even digit and how many times the digit occurs in a given integer.

#include <stdio.h>

void extract(int);
void menu(void);


int main() {
    menu();
}

void menu() {
    int userOption;
    int myValue;
    int extractDigit;

    do {
        printf("\nMENU"
            "\n1. Test the function"
            "\n2. Quit");
        scanf("%d", &userOption);

        switch (userOption) {
        case 1:
            printf("Please enter an int: ");
            scanf("%d", &myValue);

            extractDigit = digitExtract(myValue);

            break;

        case 2:
            printf("\nExiting . . . ");
            break;

        default:
            printf("\nPlease enter a valid option!");
        }
    } while (userOption != 2);
}

void digitExtract(int userValue) {
    int tempValue;
    int x;
    int myArr[10] = { 0 };

    tempValue = (userValue < 0) ? -userValue : userValue;

    do {
        myArr[tempValue % 10]++;
        tempValue /= 10;
    } while (tempValue != 0);


    printf("\nFor %d:\n", userValue);
    for (x = 0; x < 10; x++) {
        printf("\n%d occurence(s) of %d",myArr[x], x);
    }   
}

I got a program to display both odd and even numbers and its appearance.

The only part I'm stuck for is a program that displays ONLY the largest even number and its appearance. Everything that I tried, either broke the logic of the program, or produced some strange conclusion.

Any tips or ideas on how I should act?

Thanks in advance.

+4
source share
2

.

for (x = 8; x >=0; x-=2)
{
    if(myArr[x]>0) //if myArr[x]=0 then x does not exist
    {
        printf("%d occurs %d times",x,myArr[x]);
        break; //we have found our maximum even digit. No need to proceed further
    }

}

. , .

+2

? , .

(, , ):

int tempValue;
int x;
int myArr[10] = { 0 };

int maxNum = 0;

tempValue = (userValue < 0) ? -userValue : userValue;

do {
    int currNum = tempValue % 10;

    myArr[currNum]++;
    tempValue /= 10;

    if (currNum % 2 == 0 && currNum > maxNum)
        maxNum = currNum;
} while (tempValue != 0);

maxNum , myArr[maxNum] .

0

All Articles