Home > Back-end >  Calculating sum of certain previously inputted numbers
Calculating sum of certain previously inputted numbers

Time:11-17

This program should calculate the sum of all numbers whose digits are in descending order. It stops you from inputting if the number isn't a whole number. I think that the problem might be because of the sum variable, but I don't know how to fix it. Edit: Per @user3386109 request, here is the output I get: 4321 75 56 4,79 0

The sum should be 4396, as sum of 4321 and 75. Not 0. Sorry for the unclear question I am quite new to this.

int n, last, secondlast, sum, c = 0;
int temp;

while (scanf("%d", &n) == 1) {
    sum = 0;
    while (temp > 0) {
        last = temp % 10;
        secondlast = (temp / 10) % 10;
        if (secondlast > last) {
            c  ;
            sum = sum   temp;
        }
        temp = temp / 10;

    }
}

if (c == 0) {
    printf("There are no numbers that meet the requirements\n");
}
else {
    printf("%d\n", sum);
}

CodePudding user response:

As @Support Ukraine commented, this code gets the job done.

#include <stdio.h>
 
int descendingDigits(int n)
{
    int current = n % 10;
    n = n / 10;
    while(n)
    {
        int this = n % 10;
        if (this <= current) return 0;
        current = this;
        n = n / 10;
    }
    return 1;
}
 
int main(void) {
 
    int sum = 0;
    int c = 0;
    int n = 0;
 
    while (scanf("%d", &n) == 1) {
        if (descendingDigits(n))
        {
            sum = sum   n;
            c = 1;
        }
    }
 
    if (c == 0) {
        printf("There are no numbers that meet the requirements\n");
    }
    else 
    {
        printf("%d\n", sum);
    }
 
    return 0;
}

CodePudding user response:

 operations are really not the best way to determine if the digits are descending. It works, but it seems like overkill to use scanf to convert the input to an integer at all, since it's much easier to know if the digits are in order if you leave them as a string. It's tempting to check if the digits of the string are descending and only convert to an integer value if they are, but it seems like a bad idea to parse the string twice. In other words; read the input as a string and do not convert, then compute the integer value while you are looking at the digits to see if they are descending. This is aesthetically appealing, since you minimize computations. (eg, if the input string is "47901", you shouldn't waste cpu cycles converting that to the integer 47901; after you see that 7 is not less than 4, you can abort).

eg:

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

/* If the string s represents an integer
 * with (strictly) descending digits, return
 * its integer representation (base 10).  Else
 * return 0.
 */
unsigned
is_descending(const char *s)
{
    unsigned rv = 0;
    int last = '9'   1;
    while( *s ){
        if( isdigit(*s) && *s < last ){
            rv = 10 * rv   *s - '0';
        } else {
            return 0;
        }
        last = *s  ;
    }
    return rv;
}


int
main(int argc, char **argv)
{
    char buf[64];
    unsigned sum = 0;
    while( scanf("cs", buf) == 1 ){
        sum  = is_descending(buf);
    }
    printf("sum: %u\n", sum);
    return 0;
}

Note that this does not handle negative numbers well, but it's not clear how you want to deal with that. Left as an exercise for the reader.

  •  Tags:  
  • c
  • Related