answersLogoWhite

0

The following function will sum the digits in any positive number.

unsigned int sum_digits(unsigned int num, unsigned int base=10)

{

sum=0;

if(base>1) // base must be 2 or higher

{

while(num)

{

sum+=num%base; // add least significant digit to sum

num/=base; // shift right by base

}

}

return(sum);

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


How can you generate a palindrome from a given number?

You write the number and then follow it with the digits in reverse order.


Write a pseudo code to find the sum of digits of a given number reducing them to a single digit?

5


Write a program to find Armstrong number in visual basic 10?

An Armstrong number (or narcissistic number) for a given number of digits is a number that is equal to the sum of its own digits raised to the power of the number of digits. Here’s a simple Visual Basic 10 program that checks for Armstrong numbers: Module ArmstrongNumber Sub Main() Dim num As Integer Dim sum As Integer = 0 Console.Write("Enter a number: ") num = Convert.ToInt32(Console.ReadLine()) Dim temp As Integer = num Dim digits As Integer = num.ToString().Length While temp > 0 Dim digit As Integer = temp Mod 10 sum += Math.Pow(digit, digits) temp \= 10 End While If sum = num Then Console.WriteLine(num & " is an Armstrong number.") Else Console.WriteLine(num & " is not an Armstrong number.") End If End Sub End Module This program takes a number as input, calculates the sum of its digits raised to the power of the number of digits, and checks if the sum equals the original number.


Write a c program to add the digits of a given no?

THIS IS ONLY FOR UPTO A FIVE DIGIT NO. BY ROHAN ARORA#include#includevoid main(){int a,n,s,sum=0;clrscr();printf("Enter the number between 1 to 5 digits\n");scanf("%d",&a);while(a!=0){n=a%10;sum=sum+n;a=a/10;}printf("The sum of digits of the given numberis %d",sum);getch();}


Write a c program to find given number is prime or not?

Yes, do write, or if you're too lazy to your homework, use google.


How many significant digits are in for this number 3.008?

All four of the digits given are significant digits.


Could you write a assembly language program in tasm To check whether a given number present in a sequence of given memory location containing the string to be checked in 8086?

8086 assembly language program to check wether given number is perfect or not


Include zeros in 2.25 million?

Million is the name given to a number with 7 digits in front of the decimal place. In order to write out any number take the number you are given and and move the decimal place to the right until you have 7 digits in front of the decimal. add zeros to the end until it has 7 digits in front of the decimal place. So 2.25 million would be 2,250,000.


If a number with two digits is equal to four times the sum of its digits the number formed by reversing the order of the digits is 27 greater than the given number what is the number?

36


What is the smallest 5 digits odd number that can be formed with given digits 45083?

03458


Is a number with two digits is equal to four times the sum of its digits the number formed by reversing the order ofthe digits is 27 greater than the given number what is the number?

The number is 36.