Why do I get Wrong Answer?

,

Why is my program getting wrong answer?

2 Likes

Wrong answer means simply that - your program is not printing out the correct answer. You will just have to debug your program very carefully! Make sure your program is conforming exactly to the output format required, and not printing out unnecessary information. You can also read here to find out some reasons for WA and their fixes.

7 Likes

include

using namespace std;
int main(){
int i;
cin >> i;
while(i != 42){
cout << i;
cin >> i;
}
return(0);

}
Its showing wrong answer.why is it so?
4 Likes

You should print ‘\n’ after each output.

include <iostream>

using namespace std;
 int main(){ 
int i; cin >> i;
 while(i != 42){
   cout << i<<endl;
   cin >> i; 
  } 
   return(0);
}
2 Likes
#include 
using namespace std ;

int main(){

	int N ;
	cin >> N ;
	while(N!=42){
		cout << N <> N ;
	}
	return 0;
}

same code got Ac with this endl statement …

thank you very much

#include<stdio.h>
int main()
{
int x,i,j;
int a[99];
printf(“how many num you want to enter”);
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<x;i++)
{
if(a[i]!=42)
printf("%d\n",a[i]);
else
exit(1);
}
return 0;
}

import re
import math
def function(n):
i=1
sum1=1
while n:
x=math.pow(i,i)
sum1=sum1*x
i=i+1
n=n-1
sum1=int(sum1)
return sum1
def main():
sum3=0#to calculate the sum of n testcases
sum2=0
d=list()
t=int(raw_input(’’))
while t>0:
n=int(raw_input(’’))
m=int(raw_input(’’))
q=int(raw_input(’’))
try:
if n>2 and n<100001:
sum3=sum3+n
if m>1 and m<1000000001:
while q>0:
r=int(raw_input(’’))
if r>1 and r<n:
sum2=sum2+r
fu=(functionÂŽ*function(n-r))
f=function(n)/fu
res = f%m
d=res
print res
q=q-1

            except:
                    Z=1 
            t=t-1 
            return 0                                      

if name==“main”:
main()

why am i getting wrong answer on this code in pythhon
problem-easy-fambinatorial

I am able to get all the answers for the sample inputs given in the problem. But still I get “Wrong answer”.

You might be missing a few other conditions, always check your program for many different conditions. Also if in your output you give something like " The answer is 3 ", instead of simply outputting 3, you will get a wrong answer.

Its best to show your code and then ask.

Hi Arun,

Is it ok for me to show the entire code? It’s from one of the challenges problem

import java.util.Scanner;
class ChefChain
{
void compute()
{
try
{
int count1 =0,count2=0;
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
int t = sc.nextInt();
String s[] = new String[t];

int a1[] = new int[t];
for(int i = 0;i<t;i++)
{
s[i] = sc1.nextLine();

for(int j=0;j<s[i].length();j++)
{
if(j%2==0)
{
if(s[i].charAt(j)!=’+’)
{
count1++;
}
if(s[i].charAt(j)!=’-’)
{
count2++;
}
}
else
{
if(s[i].charAt(j)!=’-’)
{
count1++;
}
if(s[i].charAt(j)!=’+’)
{
count2++;
}
}
}
if(count1<=count2)
{
a1[i] = count1;
}
else
{
a1[i] = count2;
}
count1 = 0;
count2 = 0;
}
for(int i=0;i<a1.length;i++)
{
if(i>0)
System.out.println();
System.out.print(a1[i]);
}

}
catch(Exception e)
{
}
}
public static void main(String args[])
{
new ChefChain().compute();
}
}
this show wrong answer… what shud i do

CodeChef: Practical coding for everyone please check this is correct answer given for every input but why shoe wa??? ple check

tell me whats wrong in this program of finding factorials of small numbers
i am getting as wrong answer
#include<stdio.h>
int fact(int n)
{
int a;
if(n==0)
a=1;
else
a=n*fact(n-1);
return a;
}
int main()
{
int i,t,a[100],ans[100];
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&a[i]);
if((a[i]>=1)&&(a[i]<=100))
ans[i]=fact(a[i]);
}
for(i=0;i<t;i++)
printf("%d\n",ans[i]);

return 0;
}

#include<stdio.h>

void main()

{int n;
scanf("%d",&n);
while(n!=42)

{
if(n!=42)
printf("%d",n);
scanf("%d",&n);

}
}

what is wrong in this code???

you sshould use endl ornewline statement…

wasted hours rechecking my code…still cant figure out what’s wrong…
here’s my code for problem small factorials aka FCTRL2…

include < stdio.h>

int main()
{
int carry,i,j,n,m,l,sum,k;
sum=0;
scanf("%d",&n);
int num[101]={0};
for( m=0 ;m < n;m++)
{
scanf("%d",&num[m]);
}
for(l=0;l < n;l++)
{
int digit[501]={0};
digit[0]=1;
int temp;
for(j=2;j<=num[l];j++)
{
for(i=0;i<500;i++)
{
temp=(digit[i]*j)+carry;
if(temp>9)
{
digit[i]=temp%10;
carry=temp/10;
}
else if(temp<=9)
{
digit[i]=temp;
carry=0;
}
}
}
int v=500;
while(digit[v]==0)
{
v–;
}
for(k=v;k>=0;k–)
{
printf("%d",digit[k]);
}
printf("\n");
}
return 0;
}
the code even works for much larger factorials…dont know why it isnt accepted…pls help…

#include <stdio.h>
int main()

{int i,n;

for(i=0;;i++)

{scanf("%d",&n);

if(n==42)

break;

printf("%d",n);

}

return 0;

}

why does it show wrong answer

#include<stdio.h>
int main()
{
int t,n,temp,i,j,k,l,m,p,q;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n);
int a[n];
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
scanf("%d",&k);
m=a[k-1];
for(l=0;l<n;l++)
{
for(p=0;p<n;p++)
{
if(a[p]>a[p+1])
{
temp=a[p+1];
a[p+1]=a[p];
a[p]=temp;

			}
		}
	}
	for(q=0;q<n;q++)
	{
		if(m==a[q]){
		
		printf("%d\n",q+1);
		break;
	}
	}
}
return 0;

}

why is it display wromg answer