Write a C program to output the product of the the digits that make up the number.
example.123=6and1167=42

Dani AI

Generated

As asked: compute the product of the digits of a number (examples: 123 -> 6, 1167 -> 42). Several replies — notably ’s suggestion to split the number into digits and ’s push to try first — are on the right track. Below are two minimal, practical C approaches (integer-based and string-based), plus brief notes on common pitfalls and options.

Integer (arithmetic) approach — simple and fast; suitable when the input fits in a signed integer type. Initialize the product to 1 and treat 0 as a special case:

#include <stdio.h>

int main(void) {
    long long n;
    if (scanf("%lld", &n) != 1) return 0;
    if (n == 0) { printf("0\n"); return 0; }
    if (n < 0) n = -n; /* negating LLONG_MIN can overflow on some systems */
    unsigned long long product = 1;
    while (n > 0) {
        product *= (unsigned long long)(n % 10);
        n /= 10;
    }
    printf("%llu\n", product);
    return 0;
}

String-based approach — more robust: handles very long numbers, leading zeros, and input that isn’t strictly numeric. It iterates characters and multiplies digit characters only:

#include <stdio.h>
#include <ctype.h>

int main(void) {
    char buf[4096];
    if (fgets(buf, sizeof buf, stdin) == NULL) return 0;
    unsigned long long product = 1;
    int seen_digit = 0;
    for (size_t i = 0; buf[i]; ++i) {
        if (!isdigit((unsigned char)buf[i])) continue;
        seen_digit = 1;
        product *= (unsigned long long)(buf[i] - '0');
    }
    if (!seen_digit) printf("No digits found\n");
    else printf("%llu\n", product);
    return 0;
}

Notes and pitfalls: always start the accumulator at 1 (not 0); a digit 0 will make the product 0 — if zeros should be ignored, skip them (if (buf[i] != '0') product *= ...). Watch overflow: many digits of 9 make the product grow quickly (use a big-integer library or wider integer type if needed). These snippets expand on ’s advice and give concrete, testable implementations to adapt depending on input constraints.

Recommended Answers

All 5 Replies

But this is not a C problem. This is just a math. question.

what is it you want help with?

He's not asking a question, he's asking somebody to do his work for him. Vote- death

If you want a help.. Try your best.. We're not giving you the exact code. But we will giving you some tips about your problem.

Write a C program to output the product of the the digits that make up the number.
example.123=6and1167=42

Try to break your whole number into single digits and then try to find product.
if you face any problem or query please ask.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.