Home > Net >  How to start from the smallest digit?
How to start from the smallest digit?

Time:04-25

I was self studying on my Arithmetic and I encounter a problem. My goal is to find the least number not the greatest. I'm quite confused.

Example output

Enter n: 42123647
Smallest digit = 7

Program

#include<stdio.h>

int main(){
    int num1, num2, num3=0;
    printf("Enter n: ");
        scanf("%d", &num1);
        while(num1>0){
            num2 = num1 % 10;
            if(num3<num2){
                num3=num2;
            }
            num1 = num1 * 10;
        }
        printf("Smallest digit = %d",num3);
  
  return 0;
}

CodePudding user response:

On systems where sizeof (int) is 4, the statement

num1 = num1 * 10;

will quickly (within two iterations) cause signed integer overflow when the input is 42123647, as INT_MAX is 2147483647

The behaviour of this program is undefined when this occurs.


After retrieving the least significant digit with % 10, try dividing your input by 10.

Additionally, you want to check that your current digit is less than your current minimum value, and initialize your minimum value to a value greater than or equal to the maximum possible value.

#include <stdio.h>

int main(void) {
    int input;
    int min = 10;

    printf("Enter n: ");

    if (1 != scanf("%d", &input)) {
        fprintf(stderr, "Invalid input.\n");
        return 1;
    }

    while (input > 0) {
        int digit = input % 10;

        if (digit < min)
            min = digit;

        input /= 10;
    }

    printf("Smallest digit = %d\n", min);
}

CodePudding user response:

A char array (string) based solution in case your input is too large and overflows the variable as mentioned in other answers.

#include <stdio.h>
#include <ctype.h>

int isNumeric(char *str);

int main(void) {
    char *input;
    int min = 9;
    int digit;

    printf("Enter n: ");
    input = inputString(stdin,10); // explained later in answer
    if( isNumeric(input) == 0 ) {
        printf("Not a number.\n");
        return 0;
    }

    for( int i = 0; input[i] != '\0'; i  ) {
        digit = input[i] - '0'; // ascii values
        if (digit < min)
            min = digit;
    }

    printf("Smallest digit = %d\n", min);
}

int isNumeric(char *str) {
    if( str[0] == '\0' )
        return 0;
    for( int i = 0; str[i] != '\0'; i  ) {
        if( !isdigit(str[i]) ) 
            return 0;
    }
    return 1;
}

Important

inputString(stdin,10) is a function call from a function implementation that exist in this stack overflow question C-read-variable-length-input.

CodePudding user response:

As already mentioned you want to use division (instead of multiplication) to move to the next digit.

Also use a bottom-tested loop so that the case of just entering a zero as the number works correctly. A bottom-tested loop guarantees at least one pass through the loop.

The following code seems to work as long as a proper number is entered -- that is a number not preceded by a zero.

/* find a number's smallest digit

*/

#include <stdio.h>

int main (void)
{
    printf("\n");
    printf("enter n: ");
    int number;
    scanf("%d", &number);

    int smallestDigit = 9;
    do {
        int currentDigit = number % 10;
        if (currentDigit < smallestDigit) {
            smallestDigit = currentDigit;
        }
        number /= 10;                
    } while (number > 0);
    printf("\n");
    printf("smallest digit = %d\n", smallestDigit);

    return 0;
}
  •  Tags:  
  • c
  • Related