I need to write a program that read of input a number N numbric & calculate N! that's number (should be simulator of multiply into program because us want number N )
& should be Number of character for e.g char s[20].

Dani AI

Generated

The original post from is a bit unclear (as and noted). Interpreting the request as: "read an integer N and produce N! in decimal, and (if required) simulate the multiplication manually using a character/digit buffer", the practical options are:

Use a built-in integer when N is small

  • unsigned long long holds factorials up to 20! (20! = 2432902008176640000, 19 digits). A C string char s[20] is exactly tight for 20! (19 digits + terminating NUL), but char s[21] is safer. unsigned long long is simple and fast; it overflows at 21!. Example:
#include <iostream>
#include <cstdio>

int main() {
    long long n;
    if (!(std::cin >> n)) return 0;
    if (n < 0) { std::cerr << "Negative factorial undefined\n"; return 0; }
    if (n > 20) { std::cout << "Overflow for unsigned long long; use big-int method\n"; return 0; }
    unsigned long long fact = 1;
    for (long long i = 2; i <= n; ++i) fact *= i;
    char s[21];
    std::snprintf(s, sizeof(s), "%llu", fact);
    std::cout << s << '\n';
}

Use a manual (big-integer) digit array for larger N

  • Store the number as base-10 digits in an array/vector and multiply by each integer 2..N, carrying as you would by hand. This scales to arbitrarily large N (limited by RAM) and directly implements the "simulate multiply" requirement:
#include <iostream>
#include <vector>

int main() {
    int n;
    if (!(std::cin >> n)) return 0;
    if (n < 0) { std::cerr << "Negative factorial undefined\n"; return 0; }
    std::vector<int> digits(1, 1); // least-significant digit first
    for (int i = 2; i <= n; ++i) {
        int carry = 0;
        for (size_t j = 0; j < digits.size(); ++j) {
            long long prod = 1LL * digits[j] * i + carry;
            digits[j] = prod % 10;
            carry = prod / 10;
        }
        while (carry) { digits.push_back(carry % 10); carry /= 10; }
    }
    for (auto it = digits.rbegin(); it != digits.rend(); ++it) std::cout << *it;
    std::cout << '\n';
}

Notes and tips

  • 0! = 1; negative integers are invalid.
  • For performance with very large N, store digits in a larger base (like 1e9) to reduce loop iterations, or use Boost.Multiprecision/GMP for convenience.
  • To actually show each intermediate multiplication (a true step-by-step simulation), print the digit buffer and carry after each outer multiplication iteration.

Recommended Answers

All 2 Replies

EDIT: deleted my explanations because I agree with Salem...also, could you try to be a little clearer with your question (I find it difficult to understand). And try some google searches...you can easily find an in-depth explanation of factorial, C++ i/o (reading from the console), etc...

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.