I am looking for a factorial of a number greater than 10. I use long numbers, but when it comes to adding 1 to the number, (2! = 1 * (1+1)) , the robit function is not true. The question is what did I do wrong? i - the desired number, lengI - the number of digits in the number, n = 1 .

 void Plus(char *i, int &lengI, int n) { char c[1000]; int pos = 0; for (int y = lengI - 1; y >= 0; y--) { c[pos] = i[y]; pos++; } pos = 0; c[0] += n; bool change = false; while (c[pos] >= 10) { bool change = true; c[pos + 1]++; c[pos++] -= 10; } if (change) lengI++; pos = 0; for (int z = lengI - 1; z >= 0; z--) { i[pos] = c[z]; pos++; } } 
  • "then the function of robit is not true" - why do not you explain what it is "not true" is, and how does it separate from "true"? - Igor

2 answers 2

At the very beginning fill the entire array с zeros and this

 bool change = false; while (c[pos] >= 10) { bool change = true; c[pos + 1]++; c[pos++] -= 10; } if (change) lengI++; 

Rewrite so

 while (c[pos] >= 10) { int delta = c[pos] / 10; c[pos++] &= 10; c[pos] += delta; } if (pos >= lengI) lengI = pos; 

The question of the need for an array of and about whether you have enough 1000 elements leave open

  • An array of char , so you need to remember to cast to integer c[pos] - '0' . - Dimonchik0036
  • @Anton Shchyrov, then crashes outside the array - IWProgrammer
  • @Anton Shchrov, here's all the code, you can look, p.lst ru.stackoverflow.com/questions/599190/… - IWProgrammer
  • @RomaMikov If you did it В самом начале залить ВЕСЬ массив с нулями , you are placed in 1000 characters and the original array i is capable of containing a new number, then there should not be any problems. Unless declare it as int c[1000] - Anton Shchyrov

Here is a ready-made prog that solves the problem, thank you all

 #include <iostream> #include <string> using namespace std; bool IsEqual(string N, int colN, int i) { int N1 = atoi((N.c_str())); return !(N1 + 1 == i); } void Umn(char *a, int &n, char *b, int m) { char c[1000]; int i, j, l, o; //Обнуляем резултат for (i = 0; i<n + m; i++) c[i] = 0; //Умножение for (i = 0; i<m; i++) { o = 0; l = 0; for (j = n - 1; j >= 0; j--) { c[j + m - i] += (a[j] * b[m - 1 - i] + o) % 10; if (c[j + m - i]>9) { l = c[j + m - i] / 10; c[j + m - i] = c[j + m - i] % 10; } o = (a[j] * b[m - i - 1] + o) / 10 + l; if (j == 0) c[j + m - 1 - i] += o; l = 0; } } //Избавляемся от ведущих нулей n = n + m; while (c[0] == 0) { for (j = 0; j<n - 1; j++) c[j] = c[j + 1]; if (n != 1) n--; else break; } //Вывод результата for (i = 0; i<n; i++) a[i] = c[i]; } int main() { string N; cin >> N; char result[1000]; result[0] = 1; int lengRes = 1; for (int i = 2; IsEqual(N, N.size(), i); i++) { char newI[10]; string str = to_string(i); for (int p = 0; p < 10; p++)newI[p] = 0; for (int i = 0; i < str.size(); i++)newI[i] = str[i] - '0'; Umn(result, lengRes, newI, str.size()); } for (int i = 0; i < lengRes; i++) { printf("%d", result[i]); } cout << endl; system("pause"); return 0; }