 # Always getting wrong answer even though code is correct

I have tried 3 problems so far in code chef, all of which get wrong answer tag upon submission. What is up with that? Kindly help.
The latest problem : Contest Page | CodeChef
My solution : -
#include <stdio.h>
#include <math.h>

int main(void) {
int n, m, a, b, c, d, nd, T, t, i = 1, j, l, s = 0, k = 0, z;

``````printf("Enter the number of times you wanna do this shit.");
scanf("%d", &T);

for(c=1; c<=T; c++)
{
printf("\nEnter testcase #%d's number. ", i);
scanf("%d", &n);
if(-10<n && n<10)
{
k = 1;
printf("\nNumber of digits = %d", k);
}
else
{
for(i=1; k!=n; i++)
{
t = pow(10,i);
k = n % t;
}
nd = i - 1;
printf("\nThe number of digits = %d", nd);
}

for(j=nd, m=0; j>=1; j--, m++)
{
a = pow(10,j);
b = pow(10, j-1);
k = n%a;
l = n%b;
d = pow(10, m);
z = ((k-l)/b)*d;
s = s + z;
}
printf("\nThe reversed number is = %d.", s);
}
return 0;
``````

}

Thanks a lot!

Dude! please don’t print “enter the number” and all that kinds of stuff

Here is how we need to Implement

``````int main(void) {
int n, m, a, b, c, d, nd, T, t, i = 1, j, l, s = 0, k = 0, z;
scanf("%d", &T);
for(c=1; c<=T; c++)
{
char a;
scanf("%s",a);
for(int i=strlen(a)-1;i>=0;i--)
{
print("%c",a[i]):
}
}
``````

Bear in mind that your submitted entry for a CodeChef problem is not being looked at by a person; a computer program is assessing your output for matching exactly to the requirements. So no input prompts, no descriptive text around the results, and make sure text outputs if any are exactly as given, including matching upper and lower case.

The part of programming you can work on here is about making the algorithms work, and (eventually, hopefully) work efficiently. The part of programming concerned with giving good supporting information to users, validating values etc. isn’t really covered here, and establishing an accurate specification is mostly just a matter of reading the problem very carefully (and occasionally checking the sample output to clarify).

3 Likes

Character array of size 10^5 ? You are (declaring but not) using memory that is 10^5 times bigger than needed.

Also, your code doesn’t work for inputs ending with 0’s.

1 Like

So, first of all, you are not required to give descriptive statements for the input, output, or any other “shit”.

Secondly, before coding the solution, try to read the constraint ranges and the sample input/output first.

• N starts with 1, so you don’t have to check for negative numbers
• You have to check if the number has trailing zeros as you don’t have to print them in the reversed form.

Simplified approach:

1. Get rid of any trailing zeros (if present)
2. while N>0 → print N%10 (print the last digit, “%”->remainder operator) → N=N/10 (update N)

Yeah, Thanks! My bad Actually I didn’t look into ending with zeroes case, if we want to do it with strings there is no string data type in C so I took 10^5 bytes we can still do it with int also
For Code with strings:

``````#include <stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a;
scanf("%s",a);
int fl=0;
for(int i=strlen(a)-1;i>=0;i--)
{
if(a[i]!='0'&&fl==0)
{
fl=1;
}
if(fl==1)
{
printf("%c",a[i]);
}
}printf("\n");
}

return 0;
}

``````

For Code With int data type:

``````

#include <stdio.h>
#include<string.h>
int fun(int n)
{
int ans=0;
while(n)
{
ans=(ans*10)+(n%10);
n/=10;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",fun(n));
}

return 0;
}

``````