B
B
BenderF2022-01-24 04:53:18
C++ / C#
BenderF, 2022-01-24 04:53:18

Cast. How to convert BigIntenger to String and count the number of characters?

Hello !
The question is how to convert a BigIntenger to a String and count the number of characters in it?
The program calculates the factorial of an arbitrary number, DOES NOT display the result (number) on the screen, since it may be too large, but displays how many characters (digits) are in this number.
This is easy to do in Python.

import math
while True:
    x = int(input("Введите число для вычисления факториала:  "))
    fc = math.factorial(x)
    print("Факториал числа",x, "Цифр в числе - ", len(str(fc)))

And in C# it is not clear to me. Problem probably in the correct coercion of types?
using System.Numerics;

Console.WriteLine("Hello, World!");

BigInteger n = 4;               // количество циклов в факториале
BigInteger factorial = 1;            // значение факториала

for (BigInteger i = 2; i <= n; i++)
{
    factorial *= i;
}

Console.WriteLine(factorial.ToString());              // Преобразовать число в строку
Console.WriteLine(factorial.GetBitLength());      // Подсчитать количество сиволов в строке

In general, do not pay attention to the last 2 lines, I'm confused here, what and where to output and how to count the number of characters

PS I'm not a programmer, it's so interesting for the sake of development at the moment.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2022-01-24
@caballero

toString should output a string with a number
and the number of characters is the length of the string .length

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question