Hello all !

The problem that we will be taking up is http://www.codechef.com/problems/FCTRL2/ ðŸ™‚

This problem basically asks you to calculate the factorial of a number up to 100. Now, I guess most of you know what a “factorial” is. For those who don’t, the factorial of a number N is 1*2*…*N. This problem would be very simple, had it not been for the maximum value of N. The structure of the problem is such that it asks the user to take the number of test cases as the first input. Then ‘t’ integers follow where ‘t’ is the number of test cases which was given as input previously.

For every integer here, we have to calculate the factorial. This is very simple in languages like python or java which have built-in support for big integer types. It proves to be a hassle for people using C / C++ or languages that do not have a built-in biginteger type. Let’s think about how we can store the the result.

Now, the maximum number that we can store in an unsigned 32 bit integer is 2 ^ 32 – 1 and in an unsigned 64 bit integer is 2 ^ 64 – 1. Something like 100!(‘!’ is the notation

for factorial) has over 150 decimal digits. The data types mentioned earlier can store numbers having at most 9 and 19 decimal digits respectively. So, we need to find a way to store the 150+ digits that we will get as the answer. The simplest data structure that we can use is an integer array of size of about 200 to be on the safe side.

In the simplest form, let us store one decimal digit per array index. So, if the number is say 120, then the array will have the numbers as follows:

Say *a*[200] is how we declare the array, then

*a*[0] = 0

*a*[1] = 2

*a*[2] = 1

The least significant digit is stored in the lowest index 0. The next one in index 1 and so on. Along with the array, we need an integer specifying the total number of digits in the array at the given moment. Let this number be ‘*m*‘. Initially, *a*[0] will be 1 and the value of ‘*m*‘ will be 1 specifying that we have just one digit in the array.

Let’s take a simple example first. Consider that the array has some value like 45 and we need to multiply it with a value 37. This can be done in the following way.

**The array will be:**

*a*[0] = 5

*a*[1] = 4

and the value of *m* will be 2 specifying that there are 2 digits in the array currently.

Now, to multiply this array with the value 37. We start off from the index 0 of the array to index 1. At every iteration, we calculate 37 * *a*[index]. We also maintain a temporary variable called *temp* which is initialized to 0. Now, at every step, we calculate *x = a[index] * 37 + temp*. The new value of *a*[index] will be *x* % 10 and the new value of *temp* will be *temp / 10*. We are simply carrying out multiplication the way it is carried out usually. So, for the current situation, the iterations will be something like this.

Initialize *temp* = 0

**Iteration 1 : **

array = **(5, 4)**

temp = 0

index = 0, a[index] = 5

x = a[index] * 37 + temp = 5 * 37 + 0 = 185.

the new value of a[index] = 185 % 10 which is 5 and the new value of temp is 185 / 10 which is 18

**Iteration 2 :**

array : **(5, 4)**

temp = 18

index = 1, a[index] = 4

x = a[index] * 37 + temp = 4 * 37 + 18 = 166.

the new value of a[index] = 166 % 10 which is 6 and the new value of temp is 166 / 10 which is 16

We have finished 2 iterations and this is the value of ‘*m*‘, the array size at the moment. The required number of iterations is now over, but the value of *temp* is still greater than 0. This means that the current value of *temp* is to be incorporated into the array. For that, we keep appending the last digit of *temp* to the array and divide *temp* by 10 till *temp* becomes 0. So, we will get something like

**Iteration 1 : **

temp = 16 , array = **(5, 6)**

So, we add 16 % 10 to the array so that the array becomes** (5, 6, 6)** and we divide temp by 10 so that temp becomes 1. We update the value of ‘m’ to m + 1 that is m = 3

**Iteration 2 :**

temp = 1, array = **(5, 6, 6)**

Now, we add 1 % 10 to the array so the array becomes **(5, 6, 6, 1)** and we divide temp by 10 so that temp becomes 0. We update the value of ‘m’ to m + 1 that is m = 4

The value of *temp* is now 0 and our multiplication is now over. The final array we get is **(5, 6, 6, 1)**

Voila, we have the answer to 45 * 37 in our array with the Least significant digit in the 0th position. ðŸ™‚

For finding the factorial, we need to carry out this exact multiplication operation at every step as we loop from 1 to N. At the end of the Nth iteration, our array will contain

the answer and the value of m will be the number of digits in the answer. We can then just print the array from the Most significant digit to the least for the answer.

The basic flow of the program will be as below :

Start

Take in the number of test cases

While there is a test case remaining to be handled

Â Â Take in the number whose factorial is to be found, let it be N

Â Â Initialize the array's 0th index to 1 and m to 1

Â Â Initialize i to 1

Â Â While i is less than or equal to N

Â Â Â Â Carry out multiplication of the array with 'i' as shown above.

Â Â Print the contents of the array starting from the most significant digit and ending with the least significant digit.

Stop

Certain improvements can be made to the above mentioned method. We are storing only one digit per array index, We can store more than 1 digit per index so that the number of computations are reduced. The method to do that is the same as above. We leave it to the reader as an exercise ðŸ™‚

u said it wil be easier for java but java also doesent support to store factorial of integer more than 26 we need to use double

u said it wil be easier for java but java also doesent support to store factorial of integer more than 26 we need to use double

I suppose double will make you lose the precision, wouldn’t it? This is supposed to be easier in java because java lets you use java.math.BigInteger and calculations using BigInteger are reasonably fast.

u said it wil be easier for java but java also doesent support to store factorial of integer more than 26 we need to use double

//Whats wrong in this???

//working perfectly for all inputs in my comp(Dev c++)

//but showing mismatch of output here

#include

using namespace std;

int main()

{

long x,y;

int n,n1,c,i,t,o[200],m,temp;

cin>>t;

for(c=0;c>n;

Â Â n1=n;

Â Â while(n1/10 || n1>0)

Â Â Â {

Â Â Â Â o[m]=n1%10; Â Â Â Â Â Â Â Â Â //storing the Â digits of number

Â Â Â Â m++;

Â Â Â Â n1=n1/10;

Â Â Â }

//calculating the factorial

Â Â while(n>1)

Â Â Â Â {

Â Â Â Â Â Â temp=0;

Â Â Â Â Â Â for(i=0;i0)

Â Â Â Â Â Â {

Â Â Â Â Â Â Â Â m–;

Â Â Â Â Â Â Â Â cout<<o[m]<<"n";

Â Â Â Â Â Â Â Â Â

Â Â Â Â Â Â }

Â Â Â Â Â Â Â

}

return 0;

}

i understood the solution to the multiplication part…

but i dont see how looping would result in an answer…

take factorial of 100.

first number is 001 … each multiplied with 99 using the above method would give the storage as 0099.

now, wont v consider 9900 for the next part which is to be multiplied with 98…

so this way the number 9900 would keep on increasing and exceed the long storage anyhow…

so how is this algo working for factorial of 100…

if anyone could explain…??

When we get 0099 (9900) in storage (array), we have to multiply it by 98 next, so we will keep 98 in an integer variable and 0099 in the array only.

This method stores one of the two multiplicands in an array and the other in an integer variable, so we’ll keep the bigger one in the array and smaller one in integer variable.

//whats wrong in this…

#includeint main(){int i,n,a[100],j;double b[100];scanf(“%d”,&n);for( i=0;i<n;i++){scanf("%d",&a[i]);}for(i=0;i0;j–){b[i]=b[i]*j;}printf(“%lf”,b[i]);printf(“n”);}return 0;}

//whats wrong in this..???

#includeint main(){int i,n,a[100],j;double b[100];scanf(“%d”,&n);for( i=0;i<n;i++){scanf("%d",&a[i]);}for(i=0;i0;j–){b[i]=b[i]*j;}printf(“n”);printf(“%lf”,b[i]);}return 0;}

is it not a wrong way of programming?

Please tell me, what is the wrong in this program and how the way is wrong. I am new in the programming.. So please help me. Why it is giving wrong output result while submitting the solution.

Dear Rohit, Kindly post your question on discuss.code.chef.com. We also will request you to avoid posting code in the comment section of blog.

ok..sorry for that

#include

void print();

void fact();

int factorial[100];

int length;

int main()

{

int testcase,n;

scanf(“%d”,&testcase);

while(testcase–)

{

scanf(“%d”,&n);

if(n==1)

printf(“%dn”,n);

else

{

fact(n);

print();

}

}

return 0;

}

void fact(int n)

{

int temp,j,i;

length=0;

for(i=1;i<100;i++)

factorial[i]=0;

factorial[0]=1;

for(i=2;i90)

// printf(“nlength=%d temp=%dn”,length,temp);

if(length9)

{

length+=2;

factorial[j]=temp%10;

factorial[++j]=temp/10;

}

else if(temp>0)

{

length++;

factorial[j]=temp;

}

break;

}

}

}

}

void print()

{

int i;

for(i=length;i>=0;i–)

{

printf(“%d”,factorial[i]);

}

printf(“n”);

}

Please tell me why it is giving wrong answer while submitting. I have check each and every test cases.

you said it is simple in java but i tried it not working for 100!

Use BigInteger

whats wrong???

#include

int main()

{

int i,t,n[200];

double m[200];

scanf(“%d”,&t);

for(i=0;i<t;i++)

{

scanf("%d",&n[i]);

m[i]=1;

while(n[i]!=1 && n[i]!=0)

{

m[i]=m[i]*n[i];

n[i]–;

}

}

for(i=0;i<t;i++)

{

printf("%gn",m[i]);

}

return 0;

}

#include

int main()

{

int i,t,n[200];

double m[200];

scanf(“%d”,&t);

for(i=0;i<t;i++)

{

scanf("%d",&n[i]);

m[i]=1;

while(n[i]!=1 && n[i]!=0)

{

m[i]=m[i]*n[i];

n[i]–;

}

}

for(i=0;i<t;i++)

{

printf("%gn",m[i]);

}

return 0;

}

Hey, Kindly post your query on our discussion forum http://discuss.codechef.com/. You will get suitable reply to your question. ðŸ™‚

what could be possibly wrong

getting correct ans.

used double in c

chkd for 0 and 100 also

Hi Ankit, can you post your query in detail on our discussion forum http://discuss.codechef.com/.

The reason why codechef judge is giving a wrong answer to the case when using double is because double returns a number with say 6 places to decimal multiplied with 10 raised to the power x(say). The question on the other hand demands an exact answer to 100!.

but if i use the %.0lf then it would give the exact ans.. then what is the problem in that thing..

Wouldn’t the temp be temp=x/10?

In the language explanation just before the iterations it is written that “the new value of temp will be temp / 10” Please correct the error.

ya it’s x..i guess just slinty mistake..

ya it’s x..i guess just slinty mistake..

ya it’s x..i guess just slinty mistake..

why is it wrong if i store the factorial value in a unsigned double variable adn display using set precision…..i’m getting the expected output but its showing wrong answer in codechef..

#include

#include

#include

#include

using namespace std;

long double fact(int);

int main()

{

int t,n;

long double sol;

scanf(“%d”,&t);

while(t–)

{

scanf(“%d”,&n);

sol=fact(n);

cout<<setprecision(160)<<sol<<endl;

}

/* cout<<endl;

for(i=0;i<t;i++)

cout<<setprecision(160)<<sol[i]<<endl;

*/

return 0;

}

long double fact(int f)

{

if(f==0)

return 1;

else

return f*fact(f-1);

}

because unsigned double wont be able to hold d result of 100! which is the limit given in the question.

sir it holds the result of 100!

We can use long double then.I used long double and My program is working on ideone but not accepted on codechef.I don’t know what to do.

that code also prove your output

#include

int main()

{

int t,a[100];

int i,j,sum=1,c;

scanf(“%d”,&t);

for(i=0;i<t;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<t;i++)

{

c=a[i];

for(j=0;j<a[i];j++)

{

sum=sum*c;

c–;

}

a[i]=sum;

sum=1;

}

for(i=0;i<t;i++)

{

printf("%dn",a[i]);

}

return 0;

}

sir,my code works in my compiler but is not wrking here

#include

int main()

{

int a[200],m,n,i,j,k,c,x,l,temp;

scanf(“%d”,&n);

for(i=0;i<n;i++)

{ x=0;

m=1;

a[0]=1;

temp=0;

scanf("%d",&c);

if(c==1){ printf("%dn",c); break;}

for(j=1;j<=c;j++)

{

for(k=0;k=0;l–)

printf(“%d”,a[l]);

printf(“n”);

}

return 0;

}

I did the same thing. Works like a charm on my PC even for 200!, but CodeChef isn’t accepting it.

why this is giving wrong answer

#include

int fact(int);

int main(){

int testCase,i;

scanf(“%d”,&testCase);

int arr[testCase-1];

for(i=0;i<testCase;i++){

scanf("n%d",&arr[i]);

}

for(i=0;i<testCase;i++){

printf("%dn",fact(arr[i]));

}

return 0;

}

int fact(int a){

if(a==0||a==1){

return 1;

}

return a*fact(a-1);

}

why this is giving wrong answer ?

#include

using namespace std;

long long int facto(long long int n);

int main()

{

long long int num,fact;

long long int T;

cin>>T;

while(T–)

{

cin>>num;

fact=facto(num);

cout<<fact;

cout<<"n";

}

return 0;

}

long long int facto(long long int x)

{

if(x==1)

{

return (1);

}

else

return(x*facto(x-1));

}

http://www.codechef.com/viewsolution/5604831

why don’t we use long double to store the factorial and display that with 0 precision?This is what I did #include

int main(void)

{

int i,n,x;

long double fact=1;

scanf(“%d”,&n);

for(i=0;i<n;i++)

{

scanf("%d",&x);

while(x!=1)

{

fact=fact*x;

x–;

}

printf("%.0Lfn",fact);

fact=1;

}

return 0;

}

I am getting wrong answer on codechef but it is working fine on ideone till 100 factorial at least.Plz help.

Doubles are just approximations of real numbers and not exact. So, when you will see a list of factorials till 100 ! and compare your results, they will differ by a little amount. That is why, Code chef won’t accept. You need to use integers only.

What is wrong with the below code? Codechef shows wrong answer.

Language: C, works fine in codeblocks

#include

#include

int main(void) {

int n, i, j, q, k;

scanf(“%d”, &n);

for(i=0;i<n;i++)

{

q=1;

scanf("%d", &j);

for(k=1;k<=j;k++)

{

q=q*k;

}

printf("%dn", q);

}

return 0;

}

cant we simply declare the factorial as a double ?

we can modify the output by typing “%.0lf”

what is this type of error in c where ‘multiply’ is a function name

/home/kSeeRf/ccnNA6qb.o: In function `main’:

prog.c:(.text.startup+0x18): undefined reference to `mutiply’

collect2: error: ld returned 1 exit status

import java.util.*;

class smallfact

{

public static void main(String args[])

{int s=1, b=1;

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int a[]=new int[n];

b=n;

while(n!=0)

{int k=sc.nextInt();

for(int i=k;i>0;i–)

{

s=i*s;

}

a[n-1]=s;

s=1;

n–;

}

for(int i=b-1;i>=0;i–)

{

System.out.println(a[i]);

}

}

}

what is wrong with my code? why is codechef giving me wrong answer whereas my pc is giving the correct result.

import java.util.*;

class smallfact

{

public static void main(String args[])

{int s=1, b=1;

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

int a[]=new int[t];

b=t;

if(t>=1 && t0;i–)

{

s=i*s;

}

a[t-1]=s;

s=1;

t–;

}

for(int i=b-1;i>=0;i–)

{

System.out.println(a[i]);

}

}

}

}

what is the problem here?

int cannot store all the numbers. It has a range of -2,147,483,648 to 2,147,483,647, and hence it won’t store any number that lies outside this range. So when you are calculating factorial, you int s wont be able to store the values above a particular number and the program will give an error or the data will be distorted. This may be the problem you are getting when trying to submit.

I tried a biginteger implementation in c++ but is unable to submit it.

I dont know why it is not submitting.

Here is my code

http://ideone.com/tdYVS3

For C users u can try this code

#include

int main()

{ int a[200],t,n,n1,i,m,x,temp;

scanf(“%d”,&t);

while(t) //for test conditions

{ scanf(“%d”,&n);

n1=n;

i=0;

m=0;

while(n1!=0) //copying digits to array eg. for 345- a[0]=5,a[1]=4,a[2]=3

{ a[i]=n1%10;

n1=n1/10;

i++;

m++;

}

while(n!=1) //for calculating factorial ….By repeatedly multiplying digits by n-1

{ temp=0;

i=0;

while(i!=m) //multiplying digits with (n-1)

{ x=a[i]*(n-1)+temp;

a[i]=x%10;

temp=x/10;

i++;

}

while(temp!=0) //if temp is not zero then copy its digits to array

{ a[m]=temp%10;

temp=temp/10;

m++;

}

n–;

}

i=m-1;

while(i!=-1) //for displaying result..

{ printf(“%d”,a[i]);

i–;

}

printf(“n”);

t–;

}

return 0;

}

import java.util.*;

class smallfact

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

if(t>=1 && t<=100)

{

while(t!=0)

{int n=sc.nextInt();

int s=1;

for(int i=1;i<=n;i++)

{

s=s*i;

}

System.out.println(s);

t–;

}

}

}

}

what is the problem in this code?

100! is nearly 10^157 .so such big number can not be handled with even long data type .so an array can be used in this case.

can anyone explain my solution is wrong .. i am getting output in my ow dev c++ compiler but when i post it here it says wrong answer

#include

int fact(int);

int main()

{

int t,n,i;

scanf(“%d”,&t);

for(i=0;i<t;i++)

{

scanf("%d",&n);

printf("%dn",fact(n));

}

return 0;

}

int fact(int x)

{

if(x!=1)

return x*fact(x-1);

else return 1;

}

Because 100! is so large it cannot be stored in any data type available in C++.

We can use double in c

Why is this giving a wrong answer?

import java.math.BigInteger;

import java.util.Scanner;

public class SmallFactorials {

public static BigInteger factorial(int n){

BigInteger fact = new BigInteger(“1”);

BigInteger inc = new BigInteger(“1”);

if(n==0)

return BigInteger.ONE;

for(int i = n;i>0;i–){

fact= fact.multiply(inc);

inc = inc.add(BigInteger.ONE);

}

return fact;

}

public static void main(String[] args) {

int t;

System.out.println(“Enter no. of test”);

Scanner s = new Scanner(System.in);

t = s.nextInt();

int[] input = new int[t];

BigInteger []output = new BigInteger[t];

for(int i=0;i<t;i++){

input[i]=s.nextInt();

output[i]=factorial(input[i]);

}

for(int i=0;i<t;i++){

System.out.println(output[i]);

}

s.close();

}

}

Plz have a look nd tell me what am I doing wrong

#include

#include

/*

*

*/

int main(int argc, char** argv) {

int t, i;

scanf(“%d”, &t);

int a[t];

for(i=0; i<t; i++) {

scanf("%d", &a[i]);

}

for(i=0; i<t; i++) {

printf("%dn", fact(a[i]));

}

}

int fact(int x) {

int i, ans=1;

for(i=1; i<=x; i++) {

ans *= i;

}

return ans;

What;s wrong in this code?

#include

#include

int main() {

int t, i;

scanf(“%d”, &t);

int a[t];

for(i=0; i<t; i++) {

scanf("%d", &a[i]);

}

for(i=0; i<t; i++) {

printf("%dn", fact(a[i]));

}

}

int fact(int x) {

int i, ans=1;

for(i=1; i<=x; i++) {

ans *= i;

}

return ans;

I dont know whats wrong with this code. But codechef showing it wrong for the small factorial.

url:https://www.codechef.com/problems/FCTRL2

#include

using namespace std;

int main()

{

int t; cin>>t;

int n[t],i,fact;

for(i=1;i>n[i];

}

for(i=1;i<=t;i++)

{

fact=1;

for(int j=1;j<=n[i];j++)

{

fact*=j;

} cout<<fact<<endl;

}

return 0;

}

can anyone check please.

give me some tips.

codes are changed in this.

im getting correct output in code::blocks

but not working in codechef

becuase ur code will not work after the number 33

can u explain me the dry run

#include

int main()

{

int T,t,n,i;

scanf(“%d”,&T);

for( t=0 ; t<T ; t++ )

{

int N,k,n,tmp,tmp1,a[200]={0},j;

scanf("%d",&N);

a[0]=1;

j=1;

for( i=1 ; i<=N ; i++ )

{

n=i;

tmp=0;

tmp1=0;

for( k=0 ; k=0 ; i– )

{

printf(“%d”,a[i]);

}

printf(“n”);

}

return 0;

}

#include

#include

using namespace std;

int main()

{

int j,t;

cin>>t;

for(j=1; j>n;

for(i=1; i<=n; i++)

{

factorial=factorial*i;

}

cout<<factorial<<"n";

}

return 0;

}

why my code is not accepting?although i have got the right answer , it is not accepting my answer.what is wrong with my code??plz. explain

it says wrong answer, can anyone explain where is it wrong ??

#include

long fact(int x)

{

long p,fact=1;

int i;

for(i=1;i=1&&t<=100)

{

for(j=0;j<t;j++)

{

scanf("%d",&n);

if(n=1)

{

r=fact(n);

a[i]=r;

i++;

}

else return 0;

}

for(i=0;i<t;i++)

{

printf("%ldn",a[i]);

}

}

return 0;

}

how to change this code to the answer for the above question plz help

#include

using namespace std;

void fact(int n)

{

int num=1,i;

for(i=1;i<=n;i++)

{

num=num*i;

}

cout<>t;

while(t!=0)

{

cout<>n;

fact(n);

t–;

}

}

my answer is wrong ,i cant find the mistake ,someone tell the mistake.

here is the code:

#include

int fact(int x);

int main()

{int n,i,a[100];

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{scanf("%d",&a[i]);}

for(i=1;i<=n;i++)

{printf("n%d",fact(a[i]));}

return 0;}

int fact(int x)

{if(x==0)

return(1);

else

{return(x*fact(x-1));}}

The maximum value an int can store is ~ 2 x 10^9. which is not enough if you want to store factorial of 100. ðŸ™‚

can u suggest us a way to overcome this problem.

That is what this article is about, overcoming primitive data type restrictions of languages such as C/C++ using an array.

in the declaration of fact your just writing int x but it should either be a pointer of integer type or you have to declare in this way i.e x[], in the base condition your also should write whenx==1

Can any1 plz tell me why my answer is wrong?

https://www.codechef.com/viewsolution/10994804

#include

#include

#define m double

m fact(m n)

{

m i,ans=1;

for(i=2;i<=n;i++)

ans=ans*i;

return ans;

}

int main()

{

m t,*n;

int i;

scanf("%lf",&t);

n=(m*)malloc(sizeof(m)*t);

for(i=0;i<t;i++)

scanf("%lf",&n[i]);

for(i=0;i<t;i++)

printf("n%0.0lf",fact(n[i]));

return 0;

}

i am getting an error when it runs fine on my compiler.

#include

int main()

{ int a,i,b;

scanf(“%d”,&a);

int c[a];

for(i=0;i<a;i++){ scanf("%d",&b);c[i]=fact(b);}

for(i=0;i<a;i++) printf("%dn",c[i]);

return 0;

}

int fact(int n)

{int i,s=1;

for (i=1;i<=n;i++) s=s*i;

return (s);

}

please tell the error

it would not work for n>33

#include

long double factorial(int x);

int main()

{

int arr[200],k,j;

long double xfact;

scanf(“%d”,&k);

for(int i=0;i<k;i++)

{scanf("%dn",&arr[i]);

long double fact=1;

int count=1;

for(count=1;count<=arr[i];count++)

fact=fact*count;

printf("%1.0Lfn",fact);

continue;

return 0;

}}

This code works absolutely fine for finding factorial upto 175

but still it shows wrong answer in code chef and correct in other IDE

why didn’t you just use Recursion of a function to calculate factorials ?

because it would not work after number 33

https://uploads.disquscdn.com/images/c479b965f2f9b6eaf0d0d222fc84fc0a947de7094ee1e0fec9d8bc9884726e60.jpg

#include

int main()

{

unsigned long int t,n[100],i,k,f=1;

scanf(“%u”,&t);

while(t=1)

{

{

for(i=1;i<=t;i++)

{

scanf("%u",&n[i]);

printf("nn");

}

for(i=1;i<=t;i++)

{

if(n[i]=1)

f=1;

for(k=n[i];k>=1;k–)

f = f*k;

printf(“n”);

printf(“%u”,f);

continue;

}

return 0;

}

}

}

WHERE IS THE FAULT EHOUGH IT IS NOT WORKING FOR NUMBER GREATER THAN 33 BUT IT CANT BE THROUGHLY REJECTED

KYA MAJAK HAI

JJJ

is this the way of coding…

/*small factorial code chef program-5 */

#include

int main()

{

void multiply(long int n);

long int *b,t,i,fact,j;

scanf(“%ld”,&t);

if(1<=t&&t<=100)

{

b=(long int *)malloc(t*sizeof(long int));

for(i=0;i<t;i++)

scanf("%ld",(b+i));

for(i=0;i<t;i++)

{

if(0<=b[i]&&b[i]0)

{

for(i=0;i0)

{

while(temp)

{

count++;

a[count-1]=temp%10;

temp=temp/10;

}

}

p–;

}

for(i=count-1;i>=0;i–)

printf(“%ld”,a[i]);

printf(“n”);

}

The code that i have written run correctly.This problem is very nice to solve.Thank you code chef.If any of you have any doubt just ask me.

sir can you tell me why the compiler is saying that “wrong answer”. plzz point out the mistakes

#include

int fact(int n)

{

int z;

if(n==1)

return 1;

else

{

z=n*fact(n-1);

return z;

}

}

int main()

{

int l,n,*val,i;

scanf(“%d”,&n);

val=(int*)malloc(sizeof(n));

for(i=0;i<n;i++)

{

scanf("%d",&val[i]);

l=fact(val[i]);

printf("%d",l);

printf("n");

}

return 0;

}

Can we divide/modulo very large numbers using this method? If so, how?

Sir, my code gives correct output but u r website says wrong answer. plz correct me.

#include

int fact(int j)

{

if(j==1)

return 1;

return j*fact(j-1);

}

int main()

{

int T, i, j;

scanf(“%d”, &T);

for(i=0;i<T;i++)

{

scanf("%d",&j);

printf("%dn",fact(j));

}

return 0;

}

this website correct / resolve your code or not?

Please tell why is this code give runtime error:-

def fact(a):

if(a<=1):

return 1

else:

return a*fact(a-1)

b=input()

for i in range(b):

c=input()

d=fact(c)

print(d)

the maximum number that we can store in an unsigned 32 bit integer is 2 ^ 32 â€“ 1 and in an unsigned 64 bit integer is 2 ^ 64 â€“ 1. Something like 100!(â€˜!â€™ is the notation

for factorial) has over 150 decimal digits.

can anyone explain this?

Great explanation. Thanks bud.

why is this code not working here?

#include

int main()

{

int i,t,j;

double c=1,b;

scanf(“%d”,&t);

int a[t];

for(i=0;i<t;i++)

scanf("%d",&a[i]);

for(i=0;i<t;i++)

{

for(j=1;j<=a[i];j++)

{

b=c*j;

c=b;

}

printf("%.0fn",c);

c=1;

}

return 0;

}

loop hole: what if x=199 then A[index]=9 and t is already having 1 or more higher value?

why there is nzec

#include

#include

using namespace std;

int main()

{

int u;

cin>>u;

while(u–)

{

int fac;

cin>>fac;

listk;

listf;

k.push_back(1);

for(int b=1;b<=fac;b++)

{

int x,t=0,i;

while(!k.empty())

{

x=b*(k.front())+t;

k.pop_front();

t=x/10;

i=x%10;

f.push_back(i);

}

while(t)

{

f.push_back(t%10);

t=t/10;

}

k=f;

f.clear();

}

while(!k.empty())

{

cout<<k.back();

k.pop_back();

}

cout<<"n";

}

return 1;

}

the main function should return 0 at the end of the program

i think here temp=x/10;

Sir, this problem talks about solving Factorial in non recursive approach,

But why can’t we use recursive approach ? It will be much faster and easier to understand.

recursion is slower then iteration

calculate recurrence relation for the time complexity of calculation factorial recursively it will be always slower then iteration

Sir this is my code and I am not able to find out my mistake so please help!

#include

main()

{

int t,fact=1,i,n;

scanf(“%d”,&t);

int a[t];

for(i=0;i<t;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<t;i++)

{

fact=1;

n=a[i];

while(n!=1)

{

fact=fact*n;

n=n-1;

}

printf("n%d",fact);

}

}

what is wrong with this code??

CODE:

import java.util.*;

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

int a[]=new int[t];

for(int i=0;i<t;i++)

a[i]=sc.nextInt();

for(int l=0;l1;k–)

{

index=0;temp=0;

for(int h=0;h0)

{

b[index++]=temp%10;

temp=temp/10;

}

count=index-1;

}

if(b[count]!=0)

for(int m=count;m>=0;m–)

System.out.print(b[m]);

else

for(int m=count-1;m>=0;m–)

System.out.print(b[m]);

System.out.println();

}

}

}

Please identify what’s the problem in the code it is working well in my ide,Kindly go through it.

import java.util.*;

class binary

{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

int test=sc.nextInt();

int arr[] = new int[100];

int i;

int f=1;

for(i=0;i<test;i++)

{

arr[i]=sc.nextInt();

factorial(arr[i]);

}

}

private static void factorial(int arr) {

int i;

int f=1;

for(i=1;i<=arr;i++)

{

f=f*i;

}

System.out.println(f);

}

}

It works fine on compiler but it shows wrong solution in code chef

#include

using namespace std;

int factorial(int a);

int main()

{

int t,n[100],m[100];

cin >> t;

for(int x=0;x> m[x];

n[x]=factorial(m[x]);

}

for(int x=0;x<t;x++){

cout << n[x] << endl;

}

return 0;

}

//factorial function

int factorial(int a){

if(a==1){return 1;}

else {return a*factorial(a-1);}

}

#include

using namespace std;

int main()

{

int n,k,j,i,a;

double fact=1;

cin>>n;

k=n;

double arr[n];

for(j=0;j>a;

for(i=1;i<=a;++i)

{

fact*=i;

}

arr[j]=fact;

fact=1;

}

for(i=0;i<k;i++)

{

cout<<"n"<<arr[i];

}

return 0;

}

what is wrong with this codethis code is able to solve the entire no. of factorials

give me a valid reason why my code is not correct.

https://uploads.disquscdn.com/images/c479b965f2f9b6eaf0d0d222fc84fc0a947de7094ee1e0fec9d8bc9884726e60.jpg

#include

using namespace std;

int main()

{

int n,k,j,i,a;

double fact=1;

cin>>n;

k=n;

double arr[n];

for(j=0;j>a;

for(i=1;i<=a;++i)

{

fact*=i;

}

arr[j]=fact;

fact=1;

}

for(i=0;i<k;i++)

{

cout<<"n"<<arr[i];

}

return 0;

}

what is wrong with this codethis code is able to solve the entire no. of factorials

give me a valid reason why my code is not correct.

U have used double bt it is said to have integer value as output

whats wrong in my code pls tell???

#include

#include

static int x,i=0;

int k,*ptr,t;

void func(int n);

int fact(int );

int main()

{int n;

scanf(“%d”,&n);

x=n;

ptr=(int*)malloc(n*sizeof(int));

func(n);

}

int fact(int n)

{

if(n==0 || n==1)

{

return 1;

}

else{

return fact(n-1)*n;

}

}

void func(int n)

{

if(n>0)

{ scanf(“%d”,&t);

ptr[n-1]=fact(t);

func(n-1);

printf(“n%d”,ptr[–x]);

}

}

sir i have applied an another log to take out the factorial of a number which is no same as it is asked in the tutotial. my code is as follows

#include

int fact(int a)

{

int p=1;

for(int i=1;i>n;

for(int i=0;i>a;

std::cout<<fact(a)<<"n";

}

return 0;

}

Is any thing wrong wuth my code,please tell if you get any such mistake.

#include

int fact(int);

int main(void)

{

int n,i,num,t;

printf(“enter how many number you want to enter”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

scanf("%d",&num);

}

t=fact(num);

return 0;

}

int fact(int x)

{

if((x==0)||(x==1))

return 1;

else

return x*fact(x-1);

}

can anyone help me by explaining where my code is wrong.also if my code is wrong can you explain me the question too .

first of all you put your t=fact(num) step in the for loop.

and after the computing the factorial you don’t print it so

in for loop at last you write printf(“%d”,t)

and at a inicillizetion level put your t’s value in the double data type

Please check for errors (gives NZEC error)

import java.io.*;

class atm

{

public static void main(String args[])throws IOException

{

BufferedReader I=new BufferedReader(new InputStreamReader (System.in));

int i,n,j;

n=Integer.parseInt(I.readLine());

int[] f=new int[n];

int[] x=new int[n];

for (i=0;i<n;i++)

x[i]=Integer.parseInt(I.readLine());

for (i=0;i<n;i++)

{

f[i]=1;

for (j=1;j<=x[i];j++)

f[i]*=j;

}

for (i=0;i<n;i++)

System.out.println(f[i]);

}

}

sir, my code is not working and i don’t seem to understand why. pls help.

#include

using namespace std;

int main() {

int t,n;

cin>>t;

int s=1;

for(int i=1;i>n;

for(int j=1;j<=n;j++)

{ s=s*j;}

cout<<s<<endl;

}

return 0;

}

sir, my code isn’t working and i don’t seem to understand why. pls help.

#include

using namespace std;

int main() {

int t,n;

cin>>t;

int s=1;

for(int i=1;i>n;

for(int j=1;j<=n;j++)

{ s=s*j;}

cout<<s<<endl;

}

return 0;

}

The program is working fine but after submit it is ,throwing wrong answer could somebody explain why..??

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main(String[] args) {

// write your code here

int fact =1;

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();

if(1 <= t && t <= 100){

for(int i = 0 ;i < t; i++){

int n = sc.nextInt();

if(1<=n && n<=100){

for(int j = 1;j<=n;j++){

fact = fact * j ;

}

}

System.out.println(fact);

fact = 1;

}

}

}

}

because factorial of 100 will have 150 digits which can’t be hold by any datatypes. you need to use an array to store such a big number

It works fine on compiler but it shows wrong solution in code chef

#include

using namespace std;

int a[200];

int main()

{int t;

cin>>t;

while(t–)

{ int n,d,r,m=0;

cin>>n;

int i=n-1;

while(n!=0)

{d=n%10;

a[m]=d;

n=n/10;

m++;}

r=m-1;

while(i>0)

{int c=0;

int temp=0;

while(c!=r+1)

{

temp=a[c]*(i)+temp;

a[c]=temp%10;

temp=temp/10;

c++;}

while(c==r+1 && temp!=0 )

{r++;

a[r]=temp%10;

temp=temp/10;

}

i– ;

}

for(int i=r;i>=0;i–)

cout<<a[i];

cout<<endl; }

}

Why does the following does not work?

#include

using namespace std;

int fact(int n){

if(n==1 || n==0)

return 1;

return n*fact(n-1);

}

int main() {

// your code goes here

int T;

cin >> T;

while(T--){

int n;

cin >> n;

cout << fact(n) << endl;

}

return 0;

}

Why does the following does not work?

#include

using namespace std;

int fact(int n){

if(n==1 || n==0)

return 1;

return n*fact(n-1);

}

`int main() {`

int T;

cin >> T;

while(T--){

int n;

cin >> n;

cout << fact(n) << endl;

}

return 0;

}

is it not right code??

#include

int main(){

int T;

int n;

int f;

scanf(“%d”,&T);

while(T–){

f = 1;

scanf(“%d”,&n);

for(int j=n;j>0;j–){

f = f*j;

}

printf(“%dn”,f);

}

return (0);

}

It is a right code for factorial but the answer is correct till 13/14 (depending on machines). After that, if you try to calculate factorial of 15 or 16, you will get garbage value. So int is not the best option here.

Why don’t we just use double and then use,

cout<<fixed<<setprecision<<fact;

I am getting answer precisely, but still getting wrong answer.