pc-ip-01.c (707B)
1 /* 2 * Question 1: 3 * Write a program to compute the sum and product of digits of an integer using user-defined functions. 4 */ 5 6 #include <stdio.h> 7 8 int sum(int); 9 int product(int); 10 11 int main() 12 { 13 int num; 14 printf("Enter the number: "); 15 scanf("%d", &num); 16 printf("\nSum of digit: %d", sum(num)); 17 printf("\nProduct of digit: %d", product(num)); 18 return 0; 19 } 20 21 int sum(int num) 22 { 23 int result = 0; 24 while (num > 0) 25 { 26 result += num % 10; 27 num /= 10; 28 } 29 return result; 30 } 31 32 int product(int num) 33 { 34 int result = 1; 35 if (num == 0) 36 { 37 return 0; 38 } 39 while (num > 0) 40 { 41 result *= num % 10; 42 num /= 10; 43 } 44 return result; 45 }