i am trying to calculate factorial of large numbers using this code but it is not running well i am unable to figure out what is wrong in code can anyone please help me out
I have edited your code tags, so the code looks cleaner to everyone who reads the post… Regarding the code itself, your idea seems to be correct, you can’t represent large integers in C/C++ using built-in types, so you are using grade school multiplication and arrays, so kudos for that already…
Regarding your code snippet itself, well, I don’t have time to debug it now, but, I have followed the tutorial that it is provided along with the problem statement here, struggled for a long while with code correctness, but managed to get accepted using an approach quite similar to yours, so here it is:
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
std::string convert(int i){
std::string s;
std::stringstream out;
out << i;
s = out.str();
return s;}
string mult(int &fact)
{
int array[200];
int m = 1; // num de digitos na array
array[m-1] = 1;
long int temp = 0;
for(int num = 1; num <= fact; num++)
{
for(int j = 0; j <= m-1; j++)
{
long int x = array[j]*num + temp;
array[j] = x%10;
//printf("%ld \n", x);
temp = x/10;
}
while(temp > 0)
{
array[m] = temp%10;
temp /= 10;
m++;
}
if(num == fact){
std::string ans = "";
for(int x = m-1; x >= 0; x--)
{
ans+=convert(array[x]);
}
return ans;
}
}
}
int main()
{
int T;
scanf("%d", &T);
for(int x = 0; x < T; x++)
{
int num;
scanf("%d", &num);
string c = mult(num);
cout << c << endl;}
return 0;
}
thanks for replying
i m new to c programming that’s why unable to understand your code
i guess it’s written in c++ and i don’t know even a little bit of c++
can u help me with my code
thanks for giving time