diff options
Diffstat (limited to 'assignments/04-sum-product-of-digits.c')
-rw-r--r-- | assignments/04-sum-product-of-digits.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/assignments/04-sum-product-of-digits.c b/assignments/04-sum-product-of-digits.c new file mode 100644 index 0000000..d49acd3 --- /dev/null +++ b/assignments/04-sum-product-of-digits.c @@ -0,0 +1,49 @@ +/* Sum and product of the digits of given number */ + +#include <stdio.h> + +int main(void) +{ + int n, m, r, sum, prod; + printf("Sum and product of the digits of a given number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + m = n; + sum = 0; + prod = 1; + do { + r = m % 10; + m = m / 10; + sum = sum + r; + prod = prod * r; + } while (m > 0); + printf("Sum of digits of %d is %d\n", n, sum); + printf("Product of digits of %d is %d\n", n, prod); + return 0; +} + +/* +Output: +Set 1: +Sum and product of the digits of a given number + +Enter a number: 0 +Sum of digits of 0 is 0 +Product of digits of 0 is 0 + +Set 2: +Sum and product of the digits of a given number + +Enter a number: 123456 +Sum of digits of 123456 is 21 +Product of digits of 123456 is 720 + +Set 3: +Sum and product of the digits of a given number + +Enter a number: 123456789 +Sum of digits of 123456789 is 45 +Product of digits of 123456789 is 362880 + +*/ + |