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 write a program to input any number and display digits which are absent in given number?

To write a program that inputs a number and displays the digits absent in it, you can follow these steps: Convert the input number into a set of its digits. Create a set of all possible digits (0-9). Subtract the set of digits from the complete set to find the missing ones. Display the missing digits. Here’s a simple example in Python: number = input("Enter a number: ") present_digits = set(number) all_digits = set('0123456789') missing_digits = all_digits - present_digits print("Missing digits:", ''.join(missing_digits))


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

Trending Questions
Why you choose quantity surveying as your course? What is the uplink and downlink frequency in 2100 band? What is a thrust bearing typically used for? Why does the resistance in a series resonant circuit have no bearing on the resonant frequency? What field does food engineering focus on? Why does plain cement concrete is used? Can a 3 phase commercial electric grill be used on single phase 120 with a converter? What are the examples of polarized capacitor? HOW TO USE A screw-thread micrometer? How does an enzyme compare to a wrench nut and bolt? What are the units and symbols for resistance? What is the principle of Resistance heating? What is the temperature coefficient of resistance of Eureka? What is the difference between one off batch mass and continuous production? What is the difference between full wave rectifier and half wave rectifier? How do you convert a program in turbo c into gcc c? Where can one find a neutral connection point? How much windenergy of the total windenergy directed at a windturbine is converted into heat and electricy and so how much of the total windenergy that was available to the windturbine is left over? What is a structured disciplined rigorous approach to process improvement consisting of five phases where each phase is linked logically to the previous phase as well as to the next phase? Wires 1 and 2 are made of the same metal. Wire 2 has twice the length and twice the diameter of wire 1. What is the ratio of the resistances of the two wires?