Home > Blockchain >  Finding Digital Root of a very big number
Finding Digital Root of a very big number

Time:03-30

So you have to do:

11 = 1 1 = 2

3578 = 3 5 7 8 = 23 = 2 3 = 5

But the problem is that the number can be very large(consist of 10,000 digits)

#include <stdio.h>

int main(){
    char buffer[10000];
    scanf("%s", buffer);
    unsigned long long int idx = 0;
    unsigned long long int result = 0;
    while( buffer[idx] != '\n' ){
        result = (int)buffer[idx]   result;
        idx  ;
    }

    printf("%lld\n",result);
}

But even with the easiest entrances it doesn't work:

Input : 11

Output: 2798 (and it always changes, but remains a 4-digit number)

Can someone explain why is this happening? And how can I summarize each digit of a very large number?

CodePudding user response:

You got that huge number becuase your program is adding the ASCII value of various characters.

Some improvements:

  • Don't use "%s", use "%<WIDTH>s", to avoid buffer-overflow
  • Use size_t to iterate through an array, instead of unsigned long long int
  • Instead of using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • always check whether scanf() input was successful or not
  • Don't check for '\n', because string from scanf() ends at both SPACES and NEWLINE.
  • adding 1 to array size for NULL terminating character
  • Use "%zu" instead of "%lld" for size_t

Final Code:

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

int main(void) {
    char buffer[10001] = {0};

    if(scanf("000s", buffer) != 1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }

    size_t result = 0;
    for(size_t i = 0; buffer[i]; i  ) {
        if(isdigit(buffer[i])){
            result  = buffer[i] - '0';
        }
        else {
            perror("only digits are valid");
            return EXIT_FAILURE;
        }
    }

    printf("%zu\n", result);
    return EXIT_SUCCESS;
}

Output:

1112
5

TRY IT ONLINE

CodePudding user response:

You can do it without occupying memory

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

int main(void) {
    int sum = 0;
    for (;;) {
        int ch = getchar();
        if (!isdigit((unsigned char)ch)) break; // leave loop with ENTER, EOF, 'a', ...
        sum  = ch - '0';
    }
    printf("sum of digits is %d.\n", sum);
    return 0;
}

Edit: see code running at ideone

CodePudding user response:

Wiki Digital Root provides a shortcut for getting the final single digit.

  • Validate your input string has only numeric digits
  • Find the sum of all digits in ASCII form
  • Make use of congruence formula to get the result.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_NUM_LEN 10000

int digitRoot (int n) {
    if (0 == n) return 0;
    return (0 == (n % 9)) ? 9 : (n % 9);
}

int main () {
    char str_num [MAX_NUM_LEN];

    printf ("Finding Digital Root\nEnter a number : ");
    if (NULL == fgets (str_num, sizeof (str_num), stdin)) {
        perror ("Reading input string");
        return 2;
    }

    int slen = strlen (str_num);
    // remove new line if found
    if ('\n' == str_num[slen - 1]) str_num[--slen] = '\0';
    // validate input
    int digitSum = 0;
    for (int ni = 0; ni < slen;   ni) {
        if (!isdigit ((unsigned char) str_num[ni])) {
            printf ("\nERROR: Invalid digit [%c]\n", str_num[ni]);
            return 1;
        }
        digitSum  = str_num[ni] - '0';
    }
    printf ("\nDigital Root is [%d]\n", digitRoot (digitSum));
    return 0;
}
  • Related