answersLogoWhite

0


Best Answer

int sum_digits (int num) {

int sum;

sum =0;

while (num) {

sum += num % 10;

num /= 10;

}

return sum;

}

Example usage:

int main (void) {

int i;

printf ("Input a 6-digit number: ");

scanf ("%6d", &i);

printf ("The sum of the digits in %d is: %d\n", i, sum_digits (i));

return 0;

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

size_t sum_of_digits (size_t num)

{

size_t sum {0};

for ( ; num != 0; num /= 10)

{

sum += num % 10;

}

return sum;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

You can get all tutorials of "C Programming" blog

Reference:cprogramming-bd.com/c_page2.aspx# extract the digit

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program that reads a six digit integer and prints the sum of its six digits?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a visual basic program to sum the odd number digits of a 12 digit code using division and Mod and while loops?

Function sum_odd_digits(ByVal number As Integer) As Integer Dim digit As Integer sum_odd_digits = 0 While number <> 0 digit = number Mod 10 If digit And 1 Then sum_odd_digits = sum_odd_digits + digit number = number / 10 End While End Function


In java How do you have a method take in an int value and return an int containing sum of the individual digits in the number using a recursive method?

Add the last digit plus the sum of all the previous digits. The base case is that if your integer only has a single digit, just return the value of this digit. You can extract the last digit by taking the remainder of a division by 10 (number % 10), and the remaining digits by doing an integer division by 10.


How do you write a Java program to accept a 3-digits integer number and print out the highest digit from the derived input?

The tricky part is getting the individual digits. There are basically two ways to do this: 1) Convert the number to a string, and use string manipulation to get the individual digits. 2) Repeatedly divide the number by 10. The digit is the remainder (use the "%" operator). To actually get the highest digit, initially assume that the highest digit is zero (store this to a variable, called "maxDigit" or something similar). If you find a higher digit, replace maxDigit by that.


How would you write a program that read an integer for display each of digit of integer in English?

Use an enum if you are using a c style language. Or a map data structure. Assign each integer an English value and then match it to what the user inputs.


What is the formula that would get the tenths digit number in a three digit integer number?

In a three digit integer number the tenthsdigit is always 0 as integer numbers are whole numbers and have no decimal part and tenths are decimal parts:tenths_digit_of_integer_number = 0I suspect you mean "How to find the tens digit of an integer number?"; this is the second from the right, so:tens_digit = (INT(number ÷ 10)) MOD 10For example, in C this would become: tens_digit = (number / 10) % 10;

Related questions

What is the smallest 3 digit numbers with unique digits?

It is -987. The smallest positive 3-digit integer with unique digits is 102.


What program can read n number must contain four- digits only and if the second left digit odd put it in an array if not ignore it?

Input the number as a string. If the string has a length of 4 and contains only digits, convert the string to an integer. If the integer is less than 1000, input another number. Otherwise, copy the integer and divide by 100 to get rid of the two least-significant digits. Divide again by 2 and take the remainder. If the remainder is 1 then the second left digit of the 4-digit integer is odd and the 4-digit integer can be added to the array, otherwise do not add it. Repeat for all n numbers.


How do you write a visual basic program to sum the odd number digits of a 12 digit code using division and Mod and while loops?

Function sum_odd_digits(ByVal number As Integer) As Integer Dim digit As Integer sum_odd_digits = 0 While number <> 0 digit = number Mod 10 If digit And 1 Then sum_odd_digits = sum_odd_digits + digit number = number / 10 End While End Function


How do you make a program that counts how many times each digit occured in the integer?

To count the number of times a digit occurs in an integer, start by initializing an array of ten counts of digits, such as int digits[10];Then, in a loop while the number is non zero, increment the element in the digits array that corresponds to the units digit, and then divide the number by ten, such as digits[number%10]++ and number/=10;int digits[10];int i;int number = some number;for (i=0; i


Name the only digit that is NOT an integer?

All digits all part of the set of integers.


What is the largest remainder that can occur when a two-digit integer is divided by the sum of its digits?

15


Suppose a 10 digit positive integer leading digit not zero is chosen at random What is the probability that the digits in this integer are NOT distinct?

The answer is 9*9!/9*109 = 0.0003629 approx.


Is 3.7 a two digit number?

Normally a 2-digit number refers to an integer with two digits, the first of which is not 0. So the answer would be NO> But it is a number with 2 significant digits.


How many positive four-digit integer palindromes have 12 as the sum of the digits of the munber?

81


What is a multi digit number?

A number with more than one digits: that is, an integer greater than 9.


Why is 999999 the largest six digit number?

Because the next larger integer is 1000000 which has 7 digits.


What if you begin with one digit integer multiply by 3 add 8 by 2 and subtract by 6?

The answer is a negative or positive integer with one or two digits.