answersLogoWhite

0

#include<iostream>

unsigned sum_of_digits(unsigned num)

{

unsigned sum = 0;

do

{

sum += num%10;

} while (num/=10);

return sum;

}

int main()

{

unsigned number = 12345;

unsigned sum = sum_of_digits (number);

std::cout << "Sum of digits in " << number << " is " << sum << std::endl;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

Write a program that prompts the user to input a positive integer It should then output a message indicating whether the number is aprime or not?

Output a prompt.Either:Read from standard input (std::cin) to an integer.Or:Read a line from standard input (std::getline()) to a string.Create a string stream (std::stringstream) to read the string.Read from the string stream to an integer.For each integer from 2 to half the entered integer:If the entered integer is divisible by the current integer:The number is not prime.Exit the program.The number is prime.Exit the program.


C program to find square of an integer?

#include#includevoid main(){long int n;printf("Please enter the number");scanf("%ld" ,&n);n=n*n;printf("Square of entered number = %ld ");}but if you want to show the entered number e.g.Square of the entered number 8=64then below is the code.#include#includevoid main(){long int m,n;printf("Please enter the number");scanf("%ld" ,&n);m=n;n=n*n;printf("Square of entered number %ld = %ld ",m,n);}


How to write a java program to extract the last digit from the input?

Assuming you've entered a multi-digit number whole number (an integer), then take the modus (%) of the number and 10. E.g., if the number input was 1234, then 1234 % 10 is 4. Thus the final digit is 4. Note that modus 10 is the same as dividing the number by 10 and taking the remainder.


How do you write a program to find number of zeros for the given number in c program?

Assuming the number is represented by a decimal integer, initialise a counter to zero, then repeatedly divide the number by 10 and until the number is zero. After each division, examine the remainder. Each time the remainder is zero, increment the counter. If the number is represented by a decimal float, repeatedly multiply by 10 until the value is an integer, then perform the previous algorithm.


write a program that will display one if you enter any number without it will display zero in c?

You can use the following C program to display &quot;1&quot; if a user enters any non-zero number, and &quot;0&quot; if the entered number is zero: #include &lt;stdio.h&gt; int main() { int num; printf(&quot;Enter a number: &quot;); scanf(&quot;%d&quot;, &amp;num); if (num != 0) { printf(&quot;1\n&quot;); } else { printf(&quot;0\n&quot;); } return 0; } This program reads an integer from the user and checks if it is non-zero or zero, then prints the corresponding output.

Related Questions

What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Write a program that prompts the user to input a positive integer It should then output a message indicating whether the number is aprime or not?

Output a prompt.Either:Read from standard input (std::cin) to an integer.Or:Read a line from standard input (std::getline()) to a string.Create a string stream (std::stringstream) to read the string.Read from the string stream to an integer.For each integer from 2 to half the entered integer:If the entered integer is divisible by the current integer:The number is not prime.Exit the program.The number is prime.Exit the program.


Is a program with no input an incorrect program?

No, as long as it calculates something, displays something, or otherwise has an output that benefits the user, it is a program. It could be a program that makes a random number, or a program that tells the user a joke from a database.


How do you Write a C program to reverse an Integer number.?

Reference:cprogramming-bd.com/c_page2.aspx# reverse number


C program to find square of an integer?

#include#includevoid main(){long int n;printf("Please enter the number");scanf("%ld" ,&n);n=n*n;printf("Square of entered number = %ld ");}but if you want to show the entered number e.g.Square of the entered number 8=64then below is the code.#include#includevoid main(){long int m,n;printf("Please enter the number");scanf("%ld" ,&n);m=n;n=n*n;printf("Square of entered number %ld = %ld ",m,n);}


Why you need to always return integer type as return value not other datatype?

That's not true. You return whatever is necessary for the logic of your program. For example, a function or method that calculates the square root of a number returns this square root. If it were to return an integer, the answer would have to be rounded to an integer, and not be very exact. In this case, such a function would have to return a floating-point number (for example, in Java, a "double") to be correct. In other cases, you might return a string, a boolean value, or even a user-defined type such as a date, a complex number, the coordinates of a point, a font definition, etc.


How to write a java program to extract the last digit from the input?

Assuming you've entered a multi-digit number whole number (an integer), then take the modus (%) of the number and 10. E.g., if the number input was 1234, then 1234 % 10 is 4. Thus the final digit is 4. Note that modus 10 is the same as dividing the number by 10 and taking the remainder.


Can 3.4 be an integer?

No, 3.4 is not an integer. It is a rational number and a real number, but not an integer.


How is a rational number that is not an integer different from a rational number that is an integer?

A rational number which is an integer can be simplified to a form in which the denominator is 1. That is not possible for a rational number which is not an integer.


Is every integer a rational number or is every rational number an integer?

Every integer is a rational number.


Is 14.5 an integer?

The number 14.5 is a decimal and not an integer. An integer is any number which is a whole number.


Is 78 an integer?

No, it is a decimal number.