C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Product of digits of a number

Introduction

The product of digits of a number is obtained by multiplying all its digits together.

Example:

  • Input: 123 → Output: 6 (1 × 2 × 3 = 6)
  • Input: 405 → Output: 0 (4 × 0 × 5 = 0)

We’ll use a for, while and do..while loop to extract digits and multiply them.

 

C Program: Product of digits of a number

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, original, digit;

    long long product = 1;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Handle 0 as a special case

    if (num == 0) {

        product = 0;

    } else {

        // Product of digits using for loop

        for (; num != 0; num /= 10) {

            digit = num % 10;   // extract last digit

            product *= digit;   // multiply

        }

    }

 

    // Display result

    printf("Product of digits of %d = %lld\n", original, product);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 123
Product of digits of 123 = 6

OUTPUT 2 :
Enter a positive integer: 405
Product of digits of 405 = 0

OUTPUT 3 :
Enter a positive integer: 0
Product of digits of 0 = 0


Explanation

  1. Read the positive number num and store it in original.
  2. Validate that the number is positive.
  3. Handle 0 as a special case (product = 0).
  4. Use a for loop:
    • Extract last digit using digit = num % 10.
    • Multiply it with product.
    • Remove the last digit using num /= 10.
  5. Print the final product of digits.

 

C Program: Product of digits of a number

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num, original, digit;

    long long product = 1;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Handle 0 as a special case

    if (num == 0) {

        product = 0;

    } else {

        // Product of digits using while loop

        while (num != 0) {

            digit = num % 10;   // extract last digit

            product *= digit;   // multiply

            num /= 10;          // remove last digit

        }

    }

 

    // Display result

    printf("Product of digits of %d = %lld\n", original, product);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 123
Product of digits of 123 = 6

OUTPUT 2 :
Enter a positive integer: 405
Product of digits of 405 = 0

OUTPUT 3 :
Enter a positive integer: 0
Product of digits of 0 = 0


Explanation

  1. Read the positive number num and store it in original.
  2. Validate that num is positive.
  3. Handle 0 as a special case (product = 0).
  4. Use a while loop:
    • Extract last digit using digit = num % 10.
    • Multiply it with product.
    • Remove the last digit using num /= 10.
  5. Print the final product.

 

C Program: Product of digits of a number

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num, original, digit;

    long long product = 1;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Handle 0 as a special case

    if (num == 0) {

        product = 0;

    } else {

        // Product of digits using do-while loop

        do {

            digit = num % 10;   // extract last digit

            product *= digit;   // multiply

            num /= 10;          // remove last digit

        } while (num != 0);

    }

 

    // Display result

    printf("Product of digits of %d = %lld\n", original, product);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 123
Product of digits of 123 = 6

OUTPUT 2 :
Enter a positive integer: 405
Product of digits of 405 = 0

OUTPUT 3 :
Enter a positive integer: 0
Product of digits of 0 = 0


Explanation

  1. Read the positive number num and store it in original.
  2. Validate that the number is non-negative.
  3. Handle 0 as a special case (product = 0).
  4. Use a do-while loop:
    • Extract the last digit using digit = num % 10.
    • Multiply it with product.
    • Remove the last digit using num /= 10.
  5. Print the final product.