When I do Build and Run, my IDE shows the correct answer, but when I test it here, it shows me Wrong Answer! I can’t just find whats wrong in this code…
#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
if (B >= A && B >= C)
printf("%d is the largest number.", B);
if (C >= A && C >= B)
printf("%d is the largest number.", C);
return 0;
}
@iker_13 Your code assumes that there will only be 3 numbers (A, B, C). But in the problem statement, it says there will be “N numbers” (where N can be anything between 1 and 100).
What is wrong with this code? Shows and runs perfectly in my IDE, but shows compilation error in toph.co.
#include <stdio.h>
int main()
{
int i,num,n,large=0;
printf("How many numbers: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nEnter number %d: ",i+1);
scanf("%d",&num);
if(num>large)
large=num;
}
printf("\n\nThe Largest Number is %d",large);
getch();
return 0;
}
Don’t write stuff like “How many numbers:” and “\nEnter number %d”. Online Judges (OJ) do not work this way.
Online Judges compile your code and then produce an output file against the input files which are known as test cases.
Then, If the output file produced by your code does match the Accepted output files stored in the OJ, your submission will be accepted.
Suppose you are told to read 2 integers and print their sum in a single line. The for input:
101 202
the desired output should be.
303
So, if the output of your code is
303
then the submission will be accepted.
But if the output of your code is:
The sum is 303
then your output does not match with the desired output and so the code will not be accepted.
So, in order to get your codes accepted, you have to output the result in the way which was explained in the problem statement.
Fix your code according to the problem statement to get Accepted verdict.
#include<stdio.h>
int main()
{
int i,n, lar, elem;
printf("Enter total number of elements\n");
scanf("%d",&elem);
printf("Enter first number\n");
scanf("%d",&n);
lar=n;
for(i=1; i<=elem -1;i++);
{
printf("n\ Enter the number \n");
scanf("%d",&n);
if(n>lar)
lar=n;
}
printf("n\ The largest number is %d",lar);
return 0;
}
@iker_13 Here are some pointers based on your previous code that you shared here:
Do not print anything other than the answer. No “Enter total number of elements\n”, no extra texts.
First take an input n. Then a loop should run n times and read n numbers. And every time you get the number, compare it with the largest one and see if the new number that you read is the largest one.
Print just the largest number at the end as printf("%d\n", largest);. Do not print anything else.