Calculate the sum of digits. If the number repeats itself, do not calculate it [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















My question is how do I check if a digit is repeated in an integer without using arrays?



For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)



My current code is:



# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;

int digit;

printf("Please enter a number: ");
scanf("%d" ,&input);

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

if(d0 < 1) {
sum += digit;
d0 = 1;
}

input /= 10;
}

printf("Sum of different digits is: %dn", sum);
return 0;
}









share|improve this question















closed as too broad by Jean-François Fabre, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

    – Swordfish
    Nov 23 '18 at 21:05








  • 1





    For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

    – Ron Segal
    Nov 23 '18 at 21:07






  • 1





    If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

    – Stephan Lechner
    Nov 23 '18 at 21:18






  • 3





    using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

    – Jean-François Fabre
    Nov 23 '18 at 21:19






  • 7





    "school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

    – rici
    Nov 23 '18 at 21:30


















-3















My question is how do I check if a digit is repeated in an integer without using arrays?



For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)



My current code is:



# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;

int digit;

printf("Please enter a number: ");
scanf("%d" ,&input);

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

if(d0 < 1) {
sum += digit;
d0 = 1;
}

input /= 10;
}

printf("Sum of different digits is: %dn", sum);
return 0;
}









share|improve this question















closed as too broad by Jean-François Fabre, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

    – Swordfish
    Nov 23 '18 at 21:05








  • 1





    For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

    – Ron Segal
    Nov 23 '18 at 21:07






  • 1





    If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

    – Stephan Lechner
    Nov 23 '18 at 21:18






  • 3





    using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

    – Jean-François Fabre
    Nov 23 '18 at 21:19






  • 7





    "school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

    – rici
    Nov 23 '18 at 21:30














-3












-3








-3








My question is how do I check if a digit is repeated in an integer without using arrays?



For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)



My current code is:



# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;

int digit;

printf("Please enter a number: ");
scanf("%d" ,&input);

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

if(d0 < 1) {
sum += digit;
d0 = 1;
}

input /= 10;
}

printf("Sum of different digits is: %dn", sum);
return 0;
}









share|improve this question
















My question is how do I check if a digit is repeated in an integer without using arrays?



For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)



My current code is:



# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;

int digit;

printf("Please enter a number: ");
scanf("%d" ,&input);

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

if(d0 < 1) {
sum += digit;
d0 = 1;
}

input /= 10;
}

printf("Sum of different digits is: %dn", sum);
return 0;
}






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 1:36









chux

85.1k874157




85.1k874157










asked Nov 23 '18 at 21:02









Ron SegalRon Segal

12




12




closed as too broad by Jean-François Fabre, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by Jean-François Fabre, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

    – Swordfish
    Nov 23 '18 at 21:05








  • 1





    For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

    – Ron Segal
    Nov 23 '18 at 21:07






  • 1





    If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

    – Stephan Lechner
    Nov 23 '18 at 21:18






  • 3





    using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

    – Jean-François Fabre
    Nov 23 '18 at 21:19






  • 7





    "school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

    – rici
    Nov 23 '18 at 21:30



















  • What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

    – Swordfish
    Nov 23 '18 at 21:05








  • 1





    For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

    – Ron Segal
    Nov 23 '18 at 21:07






  • 1





    If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

    – Stephan Lechner
    Nov 23 '18 at 21:18






  • 3





    using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

    – Jean-François Fabre
    Nov 23 '18 at 21:19






  • 7





    "school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

    – rici
    Nov 23 '18 at 21:30

















What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

– Swordfish
Nov 23 '18 at 21:05







What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?

– Swordfish
Nov 23 '18 at 21:05






1




1





For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

– Ron Segal
Nov 23 '18 at 21:07





For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)

– Ron Segal
Nov 23 '18 at 21:07




1




1





If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

– Stephan Lechner
Nov 23 '18 at 21:18





If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with 0; for each digit, increment the respective counter/flag only if this counter is still 0. At the end write d1 + 2*d2 + 3*d3 ... Ugly, but will work.

– Stephan Lechner
Nov 23 '18 at 21:18




3




3





using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

– Jean-François Fabre
Nov 23 '18 at 21:19





using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life

– Jean-François Fabre
Nov 23 '18 at 21:19




7




7





"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

– rici
Nov 23 '18 at 21:30





"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.

– rici
Nov 23 '18 at 21:30












3 Answers
3






active

oldest

votes


















5














This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:



#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}


It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:



unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}





share|improve this answer


























  • Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

    – usr2564301
    Nov 23 '18 at 21:50








  • 1





    I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

    – Gardener
    Nov 23 '18 at 21:54





















1














I have avoided for loops and arrays and function calls.



Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).



While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.



We only look at each digit one time, so duplicates are ignored.



# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;


printf("Please enter a number: ");
scanf("%d", &input);

//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;

while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}

digit++; // increment the digit
}


printf("Sum of different digits is: %dn", sum);
return 0;
}







share|improve this answer

































    -1














    A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.



    Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.



    unsigned SumUniqueDigits(unsigned n) {
    unsigned sum = 0;
    while (n) {
    unsigned lsdigit = n % 10;
    n /= 10;
    unsigned mssdigits = n;
    while (mssdigits) {
    if (mssdigits % 10 == lsdigit) {
    lsdigit = 0;
    break;
    }
    mssdigits /= 10;
    }
    sum += lsdigit;
    }
    return sum;
    }




    Alternative even faster: On re-examination - failed test code. Removed.






    share|improve this answer


























    • @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

      – chux
      Nov 24 '18 at 1:28






    • 1





      @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

      – chux
      Nov 24 '18 at 4:01


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:



    #include <stdbool.h>
    bool numberHasDigit(unsigned n, unsigned digit) {
    while (n) {
    if (n % 10 == digit) return true;
    n /= 10;
    }
    return false;
    }
    unsigned sumOfUniqueDigits(unsigned n) {
    unsigned sum = 0;
    for (unsigned digit = 1; digit <= 9; ++digit) {
    if (numberHasDigit(n, digit)) sum += digit;
    }
    return sum;
    }


    It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:



    unsigned sumOfUniqueDigits(unsigned n) {
    bool seen[10] = {false};
    unsigned sum = 0;
    while (n) {
    unsigned digit = n % 10;
    if (!seen[digit]) {
    sum += digit;
    seen[digit] = true;
    }
    n = n / 10;
    }
    return sum;
    }





    share|improve this answer


























    • Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

      – usr2564301
      Nov 23 '18 at 21:50








    • 1





      I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

      – Gardener
      Nov 23 '18 at 21:54


















    5














    This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:



    #include <stdbool.h>
    bool numberHasDigit(unsigned n, unsigned digit) {
    while (n) {
    if (n % 10 == digit) return true;
    n /= 10;
    }
    return false;
    }
    unsigned sumOfUniqueDigits(unsigned n) {
    unsigned sum = 0;
    for (unsigned digit = 1; digit <= 9; ++digit) {
    if (numberHasDigit(n, digit)) sum += digit;
    }
    return sum;
    }


    It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:



    unsigned sumOfUniqueDigits(unsigned n) {
    bool seen[10] = {false};
    unsigned sum = 0;
    while (n) {
    unsigned digit = n % 10;
    if (!seen[digit]) {
    sum += digit;
    seen[digit] = true;
    }
    n = n / 10;
    }
    return sum;
    }





    share|improve this answer


























    • Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

      – usr2564301
      Nov 23 '18 at 21:50








    • 1





      I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

      – Gardener
      Nov 23 '18 at 21:54
















    5












    5








    5







    This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:



    #include <stdbool.h>
    bool numberHasDigit(unsigned n, unsigned digit) {
    while (n) {
    if (n % 10 == digit) return true;
    n /= 10;
    }
    return false;
    }
    unsigned sumOfUniqueDigits(unsigned n) {
    unsigned sum = 0;
    for (unsigned digit = 1; digit <= 9; ++digit) {
    if (numberHasDigit(n, digit)) sum += digit;
    }
    return sum;
    }


    It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:



    unsigned sumOfUniqueDigits(unsigned n) {
    bool seen[10] = {false};
    unsigned sum = 0;
    while (n) {
    unsigned digit = n % 10;
    if (!seen[digit]) {
    sum += digit;
    seen[digit] = true;
    }
    n = n / 10;
    }
    return sum;
    }





    share|improve this answer















    This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:



    #include <stdbool.h>
    bool numberHasDigit(unsigned n, unsigned digit) {
    while (n) {
    if (n % 10 == digit) return true;
    n /= 10;
    }
    return false;
    }
    unsigned sumOfUniqueDigits(unsigned n) {
    unsigned sum = 0;
    for (unsigned digit = 1; digit <= 9; ++digit) {
    if (numberHasDigit(n, digit)) sum += digit;
    }
    return sum;
    }


    It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:



    unsigned sumOfUniqueDigits(unsigned n) {
    bool seen[10] = {false};
    unsigned sum = 0;
    while (n) {
    unsigned digit = n % 10;
    if (!seen[digit]) {
    sum += digit;
    seen[digit] = true;
    }
    n = n / 10;
    }
    return sum;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 2:23

























    answered Nov 23 '18 at 21:38









    ricirici

    158k21139207




    158k21139207













    • Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

      – usr2564301
      Nov 23 '18 at 21:50








    • 1





      I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

      – Gardener
      Nov 23 '18 at 21:54





















    • Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

      – usr2564301
      Nov 23 '18 at 21:50








    • 1





      I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

      – Gardener
      Nov 23 '18 at 21:54



















    Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

    – usr2564301
    Nov 23 '18 at 21:50







    Zero is a digit, and it can be repeated ... (Proof: 100). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)

    – usr2564301
    Nov 23 '18 at 21:50






    1




    1





    I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

    – Gardener
    Nov 23 '18 at 21:54







    I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.

    – Gardener
    Nov 23 '18 at 21:54















    1














    I have avoided for loops and arrays and function calls.



    Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).



    While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.



    We only look at each digit one time, so duplicates are ignored.



    # include "stdio.h"
    int main()
    {
    int input = 0;
    int sum = 0;


    printf("Please enter a number: ");
    scanf("%d", &input);

    //, now, subtract those values that do not exist in your number
    int digit = 1; // start at 1, since 0 does not add to the sum
    while (digit < 10) {
    // check to see if the digit is in the number
    int testInput = input;

    while (testInput > 0) {
    if (testInput % 10 == digit) {
    sum += digit;
    break; // Don't test for this digit any more, so go back to the outer loop.
    }
    testInput /= 10;
    }

    digit++; // increment the digit
    }


    printf("Sum of different digits is: %dn", sum);
    return 0;
    }







    share|improve this answer






























      1














      I have avoided for loops and arrays and function calls.



      Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).



      While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.



      We only look at each digit one time, so duplicates are ignored.



      # include "stdio.h"
      int main()
      {
      int input = 0;
      int sum = 0;


      printf("Please enter a number: ");
      scanf("%d", &input);

      //, now, subtract those values that do not exist in your number
      int digit = 1; // start at 1, since 0 does not add to the sum
      while (digit < 10) {
      // check to see if the digit is in the number
      int testInput = input;

      while (testInput > 0) {
      if (testInput % 10 == digit) {
      sum += digit;
      break; // Don't test for this digit any more, so go back to the outer loop.
      }
      testInput /= 10;
      }

      digit++; // increment the digit
      }


      printf("Sum of different digits is: %dn", sum);
      return 0;
      }







      share|improve this answer




























        1












        1








        1







        I have avoided for loops and arrays and function calls.



        Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).



        While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.



        We only look at each digit one time, so duplicates are ignored.



        # include "stdio.h"
        int main()
        {
        int input = 0;
        int sum = 0;


        printf("Please enter a number: ");
        scanf("%d", &input);

        //, now, subtract those values that do not exist in your number
        int digit = 1; // start at 1, since 0 does not add to the sum
        while (digit < 10) {
        // check to see if the digit is in the number
        int testInput = input;

        while (testInput > 0) {
        if (testInput % 10 == digit) {
        sum += digit;
        break; // Don't test for this digit any more, so go back to the outer loop.
        }
        testInput /= 10;
        }

        digit++; // increment the digit
        }


        printf("Sum of different digits is: %dn", sum);
        return 0;
        }







        share|improve this answer















        I have avoided for loops and arrays and function calls.



        Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).



        While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.



        We only look at each digit one time, so duplicates are ignored.



        # include "stdio.h"
        int main()
        {
        int input = 0;
        int sum = 0;


        printf("Please enter a number: ");
        scanf("%d", &input);

        //, now, subtract those values that do not exist in your number
        int digit = 1; // start at 1, since 0 does not add to the sum
        while (digit < 10) {
        // check to see if the digit is in the number
        int testInput = input;

        while (testInput > 0) {
        if (testInput % 10 == digit) {
        sum += digit;
        break; // Don't test for this digit any more, so go back to the outer loop.
        }
        testInput /= 10;
        }

        digit++; // increment the digit
        }


        printf("Sum of different digits is: %dn", sum);
        return 0;
        }








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 22:02

























        answered Nov 23 '18 at 21:50









        GardenerGardener

        1,5921616




        1,5921616























            -1














            A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.



            Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.



            unsigned SumUniqueDigits(unsigned n) {
            unsigned sum = 0;
            while (n) {
            unsigned lsdigit = n % 10;
            n /= 10;
            unsigned mssdigits = n;
            while (mssdigits) {
            if (mssdigits % 10 == lsdigit) {
            lsdigit = 0;
            break;
            }
            mssdigits /= 10;
            }
            sum += lsdigit;
            }
            return sum;
            }




            Alternative even faster: On re-examination - failed test code. Removed.






            share|improve this answer


























            • @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

              – chux
              Nov 24 '18 at 1:28






            • 1





              @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

              – chux
              Nov 24 '18 at 4:01
















            -1














            A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.



            Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.



            unsigned SumUniqueDigits(unsigned n) {
            unsigned sum = 0;
            while (n) {
            unsigned lsdigit = n % 10;
            n /= 10;
            unsigned mssdigits = n;
            while (mssdigits) {
            if (mssdigits % 10 == lsdigit) {
            lsdigit = 0;
            break;
            }
            mssdigits /= 10;
            }
            sum += lsdigit;
            }
            return sum;
            }




            Alternative even faster: On re-examination - failed test code. Removed.






            share|improve this answer


























            • @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

              – chux
              Nov 24 '18 at 1:28






            • 1





              @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

              – chux
              Nov 24 '18 at 4:01














            -1












            -1








            -1







            A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.



            Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.



            unsigned SumUniqueDigits(unsigned n) {
            unsigned sum = 0;
            while (n) {
            unsigned lsdigit = n % 10;
            n /= 10;
            unsigned mssdigits = n;
            while (mssdigits) {
            if (mssdigits % 10 == lsdigit) {
            lsdigit = 0;
            break;
            }
            mssdigits /= 10;
            }
            sum += lsdigit;
            }
            return sum;
            }




            Alternative even faster: On re-examination - failed test code. Removed.






            share|improve this answer















            A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.



            Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.



            unsigned SumUniqueDigits(unsigned n) {
            unsigned sum = 0;
            while (n) {
            unsigned lsdigit = n % 10;
            n /= 10;
            unsigned mssdigits = n;
            while (mssdigits) {
            if (mssdigits % 10 == lsdigit) {
            lsdigit = 0;
            break;
            }
            mssdigits /= 10;
            }
            sum += lsdigit;
            }
            return sum;
            }




            Alternative even faster: On re-examination - failed test code. Removed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 3:59

























            answered Nov 23 '18 at 21:54









            chuxchux

            85.1k874157




            85.1k874157













            • @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

              – chux
              Nov 24 '18 at 1:28






            • 1





              @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

              – chux
              Nov 24 '18 at 4:01



















            • @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

              – chux
              Nov 24 '18 at 1:28






            • 1





              @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

              – chux
              Nov 24 '18 at 4:01

















            @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

            – chux
            Nov 24 '18 at 1:28





            @rici Its right to left. 811%8 does not occur, but 81%1 and then 8%1. Code tested as functional the same as yours, just a performance difference.

            – chux
            Nov 24 '18 at 1:28




            1




            1





            @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

            – chux
            Nov 24 '18 at 4:01





            @rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.

            – chux
            Nov 24 '18 at 4:01



            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()