FCTRL2 WRONG ANSWER

**


Here is my block of code kindly tell me what i am doing wrong


**

#include <iostream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    int testcase, n, i = 1;
    long fctrl = 1;
    scanf("%d", &testcase);
    while(testcase--) {
        cin >> n;
        while(i <= n) {
            fctrl *= i;
            i++;
        }
        printf("%d\n", fctrl);
    }

    return 0;
}

What you are missing is the integer range overflow.

Go through this link for a detailed approach towards calculating factorials of large numbers .

import java.util.*;
class factorial
{
public static void main(String args[])
{
Scanner I=new Scanner(System.in);
int b=I.nextInt();

long ar[]=new long[b];
int i;
for(i=0;i<b;i++)
{
 int c=I.nextInt();
 long fact=1;
 for(int j=c;j>1;j--)
 {
     fact=fact*j;
 }
ar[i]=fact;
}
for(i=0;i<b;i++)
{
    System.out.println(+ar[i]);
}

}
}

You made at least three mistakes. First is variable overflow. Even using unsigned long long int you can store factorial only up to 22!. What is more you are trying to print long type using %d. %d is for integer, for long you should use %l. Also there is one more - I believe you should set fctrl to 1 for each testcase.
To solve problem do what @mediocoder told you.

#include
using namespace std;

int main() {
// your code goes here
int t;
long long int fact=1;
std::cin >> t;
int n[t];
for(int i=0;i<t;i++)
{
std::cin >> n[i];
}

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

int fact=1;
for(int j=1;j<=n[k];j++)
{

    fact=fact*j;
}

std::cout << fact <<"\n";
}

return 0;

}

What is the problem in this code? Kindly suggest me…

Use the link [http://www.geeksforgeeks.org/factorial-large-number/][1]
[1]: http://www.geeksforgeeks.org/factorial-large-number/

use BigInteger class since the answer can be very big. in c++ try to use a data type that is suitable for holding very large numbers. you can also study the karatsuba algorithm for implementing the same.

1 Like

#include

using namespace std;

int main()
{
int times;
double num,totale;
cin>>times;
double a[times];
double numb[times];
totale = 1;
for(int i =0;i<times;i++)
{
cin>>num;

	for(int s=0;s<num;s++)
	{
		if(num-s == 0)
		{
			break;
		}
		totale *= (num-s);
	}
	a[i] = totale;
	totale = 1;
}
for(int i =0;i<times;i++)
{
	cout<<a[i]<<endl;
}
return 0;

}

I dont know what is wrong in here!

Please let me know what is wrong with this
https://www.codechef.com/viewsolution/13594809

I have gone through the big computation tutorial and is obviously a good choice but still why wrong answer with this?

#include <stdio.h>
#include <math.h>
#include <string.h>
#define LL long long
#define ULL unsigned long long
#define max(a,b) a>b?a:b;
#define min(a,b) a<b?a:b;
#define fr(i,a) for(i = 0; i < a; i++)
#define fre(i,a) for(i = 1; i <= a; i++)
#define si(a) scanf("%d",&a)
#define sd(a) scanf("%lf",&a)
#define sl(a) scanf("%Ld",&a)
#define ss(a) scanf("%s",a)

LL gcd(LL a, LL b){
if (b == 0) return a;
else return gcd(b,a % b);
}

LL lcm(LL a,LL b){
return (a * b)/ gcd(a,b);
}

LL a[109];

double fact(double n) {
int x = n;
if(a[x]) return a[x];
else return n*fact(n-1);
}

int main() {
int t;
si(t);
while(t–) {
int n;
double ans;
si(n);
a[0] = a[1] = 1;
ans = fact(n);
printf("%.0lf\n", ans);
}
}

You cannot store more than 20! this way. After that it causes overflow.