while function C programing

Viewed 36

I'm a first year collage student and I'm working on an exercise with a while function.

my current objective is to identify the lowest and highest value digits in a given number.

this is my code so far:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void main() {
    printf("enter an integer\n");
    int one,onemax,onemin, min=9, max=0;
    scanf("%d", &one);
    onemax = one;
    onemin = one;
    while (onemax >= 0) {
        if (max <= onemax % 10) {
            max = onemax % 10;
            onemax=onemax / 10;
            if (onemax == 0) {
                break;
            }
        }
        else {
            onemax=onemax / 10;
        }
    }
    while (onemin != 0) {
        if (min >= onemin % 10) {
            min = onemin % 10;
            onemin=onemin / 10;
            if (onemin == 0) {
                break;
            }
        }
        else {
            onemin=onemin / 10;

        }
    }
    printf("min = %d max = %d", min, max);
}

now I know I'm stuck in a loop here and I need help getting out.

1 Answers

when onemax reaches 0 you are done. You should change the >= condition to >:

while (onemax > 0) {
// Here ------^
Related