Answers for "calculate sum of digits of a number"

0

adding digits of a number in c

//program to find the sum of digits:

#include<stdio.h>
int main()
{
  int num,sum=0,r,temp;
  printf("Enter the number:\n ");  //taking input from the user
  scanf("%d",&num);
  
  temp=num;           //assigning num to temporary variable
  
  while(temp!=0)
  {
    r=temp%10;
    sum=sum+r;
    temp=temp/10;
  }
  printf("\nGiven number = %d",num);
  printf("\nSum of the digits = %d",sum);
}

//code By dungriyal
Posted by: Guest on October-10-2020
0

sum of digits

num = [int(d) for d in input("Enter the Number:")]
sum = 0
for i in range(0, len(num)):
    sum = sum + num[i]

print("Sum of Digits of a Number: {}".format(sum))

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)
Posted by: Guest on May-03-2021
0

sum the digits of an integer

def getSum(n)
  n.digits.sum
end

print "Sum of digits = ", getSum(555);
Posted by: Guest on May-27-2021
-2

sum the digits of an integer

#include <stdio.h>
 
int getSum(unsigned long long n) {
    int sum = 0;
    for (; n; n /= 10)
    	sum += n % 10;
    return sum;
}

int main(){
	printf("Sum of digits = %d\n", getSum(555));
}
Posted by: Guest on May-27-2021

Code answers related to "calculate sum of digits of a number"

Python Answers by Framework

Browse Popular Code Answers by Language