What is wrong with this code?

#include <iostream.h>
using namespace std;

int main() {
// your code goes here
int t,i;
scanf("%d",&t);
for(i=t;i>0;i–)
{
int n,fact=1,j;
scanf("%d",&n);
if(n>0)
{
for(j=n;j>0;j–)
fact=fact*j;
printf("%d\n",fact);
}
}
return 0;
}
I have tried this on many custom inputs and all of the outputs are correct but when I try to submit it says wrong answer.

I think it will not print anything for 0! which is 1.

Show your question and solution link.

Please provide the question…
just copy the link and paste

the range is 1<=n<=100

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

1 Like

Logically your code is correct, It should print the factorial of input ‘n’ but the data type ‘int’ does not have enough memory to store integers of 10 or more digits. You may use ‘long long int’ but it won’t help you much either. These data types can’t hold values for something like 20 or more.

You need to design a code where you’re able to store values of that magnitude.
Method 1:
My solution: Solution: 50435332 | CodeChef
You need to use data type ‘cpp_int’ which allows you to store very large values.
You can learn more about ‘cpp_int’ from the given link below:
cpp_int - 1.53.0

Method 2:
My solution: Solution: 50436220 | CodeChef
I’ve created a vector of integers to store individual integers of every number which allows me to find factorial of very large numbers.
Do try to experiment with my code to understand it better :metal:

1 Like

you have written two statements wrong
1)in line 1 header file must be #include
And in both for loops the there must be i–(i minus minus) not i-(hiphen) or j-.

header file must be iostream not iostream.h

Thank you!!

1 Like

um = int(input())
summ = 0
ans = []
for index in range(num):
num1 = input()
for index1 in range(len(num1)):
if index1 == 0 or index1 == len(num1) - 1:
ans.append(int(num1[index1]))
print(sum(ans))
ans = []
https://www.codechef.com/problems/FLOW004
my code works perfectly for all the inputs but it is still showing wrong answer.Can anybody find out what the problem is?

1 Like

When the input is single digit, your output is the digit itself whereas it should be twice as much because the only digit is the first and also the last digit.
Your Solution corrected:
https://www.codechef.com/viewsolution/50444733

Also you are coding too many lines for using python. You can simplify your code much better.
Try This:
https://www.codechef.com/viewsolution/50444446

its easy…

#include
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cpp_int k=1;
cin>>n;
for(int i=1;i<=n;i++){
k = k*i;
}
cout<<k<<endl;
}
return 0;
}
because the output data is more than long long you will use boost multiprecision.
i am writing iostream but its not showing, please include it.

To everyone who posted their code, kind reminder:
You can format your code by pasting it between a pair of three backticks(`). Otherwise, the non-alphabetic symbols are parsed as markdown and the result is unexpected.

1 Like

I have another question:Solution: 50471312 | CodeChef
how to submit this without getting wrong answer error.

First of all, don’t worry if you’re not able to solve questions easily. You will improve with practice so keep going.
Now, Apparently you misinterpreted the question. Your output shows the number of the round in which the winner was decided and the score in that round. Now this matches with sample test case in the question but not with the hidden test cases.

You need to output the winner player (1 or 2) and you need to store the cumulative score of the players and output the difference when their cumulative score difference is maximum.

Here’s your code corrected: Solution: 50472988 | CodeChef

Here’s Mine: Solution: 50472553 | CodeChef

Try to understand them
Good luck!!!

Here every thing is correct but in this program fact is of int data type which only store from -2,147,483,647 to 2,147,483,647 . And if you check it cannot store factorial of large numbers like 50,60,…
There are two ways to solve the above problem :
first one is by using array to store the factorial
second one is by including new header file “boost/multiprecision/cpp_int.hpp” which contain cpp_int data type that can store that much large number.

oh! now I get it I printed the index value instead of player 1 or player 2.

1 Like