Information
The numbers in RED are typed by the user.Enter an integer (1 to 2,147,483,647 without commas): 1776 The sum of the digits in 1776 is 21. The product of the digits in 1776 is 294. Enter an integer (1 to 2,147,483,647 without commas): 8 The sum of the digits in 8 is 8. The product of the digits in 8 is 8. Enter an integer (1 to 2,147,483,647 without commas): 12345 The sum of the digits in 12345 is 15. The product of the digits in 12345 is 120. Enter an integer (1 to 2,147,483,647 without commas): 2048 The sum of the digits in 2048 is 14. The product of the digits in 2048 is 0.
The name of the file should be sumdigits.c and the command to compile it will look like this:
output-sumdigits.txt This output was generated by running the program 10 times, using each of these numbers:gcc -Werror -Wall -Wextra -ansi -pedantic sumdigits.c -o sumdigits -O2 -Wno-unused-result
1 42 555 1729 65535 142857 8675309 63288349 999999937 2147483647
Notes
this will give you the right-most digit, which you can then use in your calculations.value % 10
On subsequent redirects, use two greater-than symbols with no space between them (>>).sumdigits > myoutput.txt
This will append to the existing file, rather than overwrite it. Of course, you won't see any prompt because that, too, is being redirected to the file.sumdigits >> myoutput.txt
For students that want to practice their C-fu