C (programming language)

# C Program to find product of digits of a number

**Understanding the Problem**

**Goal:**Write a C program that calculates the product of the individual digits of a given integer.**Key Concepts:**- Modulo operator (
`%`

): Extracting individual digits. - Loops:
`while`

loop for repeated operations. - Mathematical calculations: Multiplying digits using the
`*`

operator.

- Modulo operator (

**Step-by-Step Guide**

**1. Header Inclusion**

C

`#`**include** <stdio.h>

**Explanation:**Includes the standard input/output library for functions like`printf`

and`scanf`

.

**2. Main Function**

C

```
int main() {
// Code statements here
return 0;
}
```

**3. Get Input Number**

C

```
int num, product = 1;
printf("Enter a number: ");
scanf("%d", &num);
```

**Explanation:**- Declares
`num`

to store the input number and`product`

to store the calculated product (initialized to 1). - Prompts the user to enter a number and reads it using
`scanf`

.

- Declares

**4. Loop to Extract and Multiply Digits**

C

```
while (num > 0) {
int digit = num % 10; // Extract the last digit
product *= digit; // Multiply the digit to the product
num /= 10; // Remove the last digit from the number
}
```

**Explanation:**`num % 10`

: Extracts the last digit using the modulo operator.`product *= digit`

: Multiplies the extracted digit to the`product`

variable.`num /= 10`

: Removes the last digit from`num`

by dividing it by 10.

**5. Print the Product**

C

```
printf("Product of digits: %d\n", product);
```

**6. Complete Code**

C

`#`**include** <stdio.h>
int main() {
int num, product = 1;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
int digit = num % 10;
product *= digit;
num /= 10;
}
printf("Product of digits: %d\n", product);
return 0;
}

**7. Executing the Program**

- Save the code as a
`.c`

file (e.g.,`product_of_digits.c`

). - Compile using a C compiler (e.g.,
`gcc product_of_digits.c -o product_of_digits`

). - Run the executable (e.g.,
`./product_of_digits`

).

**Example Output**

Enter a number: 12345 Product of digits: 120