UJ Chan
UJ Chan

Reputation: 19

How to add each digit in an integer

This is the question, I have no idea how to do it

  1. (7 points) Write an int function named AddDigit that takes an integer number as input It returns the sum of all digits which are divisible by 3 or 5. For example,

AddDigit(13579) - it returns 17 (i.e 3+5+9) because 3, 5 and 9 are divisible by 3 or 5

AddDigit(355) - it returns 13 (i.e 3+5+5) because 3 and 5 are divisible by 3 or 5

AddDigit(248) - it returns 0 because no digit is divisible by 3 or 5

And this is my code:

    #include <stdio.h>
    #include <cstring>
    #include<time.h>
    #include<iostream>
    using namespace std;
    int AddDigit(char a[]) {
        int sum = 0, numberofdigit;
        numberofdigit = strlen(a);
        for (int i = 0; i < strlen(a)-1; i++) {
            if ((a[i] % 3 == 0 || a[i] % 5 == 0)&&a[i]!=0) {
                sum += a[i];
            }
        }
        return sum;
    }
    int main() {
        char b[10];
        cin >> b;
        cout<<AddDigit(b);
     }

Upvotes: 1

Views: 1395

Answers (3)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

From reading the assignment, the AddDigit function is supposed to take an int argument, not a character array, string, or any other character-based type. Thus your approach starts out incorrectly, since it violates the assignment's requirements.

So the approach is to figure out how to strip each digit from an int, and from that digit, determine if it is evenly divisible by 3 or 5. Using simple modulus and continuous division of the passed-in integer, an approach can be something like this:

int AddDigit(int n)
{
    int total = 0; // final total
    while (n > 0)
    {
        int digit = n % 10; // get rightmost digits

        // add the value if digit is either evenly divisible by 3 or 5
        total += ((digit % 3 == 0 || digit % 5 == 0) ? digit : 0);

        // remove the last digit from the number
        n /= 10;
    }
    return total;
}

Note that the line that adds to the total will add 0 if the ternary condition returns false. That condition is to check if either the digit is evenly divisible by 3 or 5. If the condition is true, we simply add that digit onto the total.

Upvotes: 1

Kostas
Kostas

Reputation: 4176

It could be a bit more neatly done in a recursive function:

int AddDigit(int num)
{
    if (num == 0)  //WE REACH THIS WHEN 1-DIGIT NUMBER IS DIVIDED BY 10
            return 0;

    int curNum = num % 10;  //ALWAYS ACCOUNTING FOR ONLY LAST DIGIT


    //WE ADD IT OR NOT AND THEN CONTINUE TO THE NEXT DIGIT

    if (curNum % 3 == 0 || curNum % 5 == 0)
            return curNum + AddDigit(num/10);
    else
            return AddDigit(num/10);


}

Upvotes: 0

praveen
praveen

Reputation: 71

You are giving the input as a char array, a character '0' is different from an integer 0,
ASCII of '0' which is 48
ASCII of 0 which is 0
Why you got 50 for '123' because '2' means 50%5==0, so you added '2' to the sum.

To get what u wanted, you need to get the integer equivalent of the char array with d = arr[i]-'0'

   int AddDigit(char a[]) {
        int sum = 0, numberofdigit;
        numberofdigit = strlen(a);
        for (int i = 0; i < strlen(a); i++) {
            int d = a[i]-'0';
            if ((d % 3 == 0 || d % 5 == 0)&&d!=0) {
                sum += d;
            }
        }
        return sum;
    }

Upvotes: 3

Related Questions