-1

I got wrong output when I run the programme.I use python 3.4.3 version.Below is my program code,

CODE:-

from pip._vendor.distlib.compat import raw_input

def sumOfDigits(n):
    summ=0;
    while(n!=0):
        r = n%10;
        summ+=r;
        n/=10;
    return summ;


input_num = raw_input("Enter a number : ");
n = int(input_num);

print("sum of digits of the number %s is %d" % (input_num,sumOfDigits(n)));

OUTPUT:-

Enter a number : 54928
sum of digits of the number 54928 is 31
4
  • You are using Python3. Use //= instead of /=. Commented Aug 19, 2015 at 7:42
  • 1
    sum(int(value) for value in input_num) Commented Aug 19, 2015 at 7:44
  • Thanks @Amadan...it is running properly. Commented Aug 19, 2015 at 7:46
  • @Amadan, you should make that an answer so OP can accept it. Commented Aug 19, 2015 at 7:48

1 Answer 1

1

TRY:

def sumOfDigits(n):
r = 0
while n:
    r, n = r + n % 10, n / 10
return r


input_num = raw_input("Enter a number : ");
n = int(input_num);

print("sum of digits of the number %s is %d" % (input_num,sumOfDigits(n)));

Worked for me perfectly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.