whats wrong in my code of calculating factorial of large numbers

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

#include<stdio.h>
int a[100],n1,temp=0,i=0,c=0,x;

void factorial(int);
void print();
int main()
{
a[0]=1;
    printf("enter number to calculate factorial\n");
    scanf("%d",&n1);
    for(i=2;i<=n1;i++)
    {
        factorial(i);
    }

    print();
    return 0;
}
void factorial(int n2)
{
    /*while(n1!=0)
    {
        temp=n1%10;
        a[i]=temp;
        n1=n1/10;
        i++;
        c++;
    }*/
    temp=0;


    for(i=0;i<=c;i++)
    {
        x=a[i]*n2+temp;
        a[i]=x%10;
        temp=x/10;
    }
    while(temp!=0)
    {
        c++;
        a[c]=temp%10;
        temp=temp/10;

    }
}
void print()
{
    for(i=c;i>=0;i--)
    {
        printf("%d",a[i]);
    }
}

please help

Hello nishant_25,

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;
}

And I hope this can help you getting accepted,

Best regards,
Bruno

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

i figure out the problems and corrected all of them
this is the correct version

enter code here

#include<stdio.h>
int a[1000],n1,temp=0,c=0,x;

void fact(int);
void print();
int main()
{
int i,cases,j;
scanf("%d",&cases);
for(j=1;j<=cases;j++)
{
a[0]=1;
c=0;
scanf("%d",&n1);
for(i=2;i<=n1;i++)
{
fact(i);

        }
        print();
}
return 0;

}
void fact(int n2)
{
int i;
temp=0;

for(i=0;i<=c;i++)
{
    x=a[i]*n2+temp;
    a[i]=x%10;
    temp=x/10;
}
while(temp>0)
{
    c++;
    a[c]=temp%10;
    temp=temp/10;

}

}
void print()
{
int i;
for(i=c;i>=0;i–)
{
printf("%d",a[i]);
}
printf("\n");
}
enter code here

//