# Need Help! my code is successfully executed in Code,Compile & Run Panel of Codechef but on submission shows Wrong answer. The Question Code is FLOW004 (first and last digit) of Begineer level.And, my code is:

**URL:** <https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201>\
**Category:** help\
**Tags:** c-plus-plus\
**Created:** [August 27, 2020, 1:08pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201 "2020-08-27T13:08:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 27, 2020, 1:08pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/1 "2020-08-27T13:08:16Z")

</div>

[FLOW004](https://www.codechef.com/problems/FLOW004)  
[my code](https://www.codechef.com/submit/FLOW004)

```auto

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	if(t>=1 && t<=1000)
	{
	    for(int i=0;i<t;i++)
	    {
	        long n;
	        long r=0;
	        int ld=0,fd=0;
	        cin>>n;
	        if(n>=1 && n<=1000000)
	        {
	           
	            ld=n%10;
	            for(long j=0;j<n;j++)
	            {
	               r=n/10;
	               if(r<10)
	               {
	                   fd=(int)r;
	                   break;
	               }
	            }
	        }
	        cout<<fd+ld;
	    }
	}
	return 0;
}

```

---

<div class="post-metadata">

**Author:** ![krishna\_kc](https://discuss.codechef.com/user_avatar/discuss.codechef.com/krishna_kc/32/24644_2.png) [@krishna\_kc](https://discuss.codechef.com/u/krishna_kc)\
**Post date:** [August 27, 2020, 1:29pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/2 "2020-08-27T13:29:57Z")

</div>

> [@santosh\_ks15](#):
>
> if(t\>=1 && t\<=1000)

> [@santosh\_ks15](#):
>
> `if(n>=1 && n<=1000000)`

This is not your responsibility, The input used during testing is provided within the constraints ,don’t worry. Also, for last digit, you are not changing n value at each iteration, so ld will remain 0 and your program gives wrong answer.

Also, the ‘my code’ link is not the correct one, when you click submit button, your code is visible to you but not us. If you submit it, then the submission link should be shared.

Lastly use three backticks ( ` ) before and after your code so that it appears properly in post.

```auto
#include<iostream>

using namespace std;

int main() {

// your code goes here

int t;

cin>>t;

if(t>=1 && t<=1000)

{

 for(int i=0;i<t;i++)
    {
        long n;
        long r=0;
        int ld=0,fd=0;
        cin>>n;
        if(n>=1 && n<=1000000)
        {
            ld=n%10;
            for(long j=0;j<n;j++)
            {
               r=n/10;
               if(r<10)
               {
                   fd=(int)r;
                   break;
               }
               n=n/10;
            }
        }
        cout<<fd+ld<<"\n";
    }
}
return 0;
}

```

keep learning 😄

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 27, 2020, 1:48pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/3 "2020-08-27T13:48:10Z")

</div>

Thank you so much sir for going through my code.  
actually I am new here and don’t know all the features 😅

I will learn very soon sir 😊

Thanks again sir

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [August 27, 2020, 2:08pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/5 "2020-08-27T14:08:43Z")

</div>

You still haven’t correctly linked to your original submission, and the code in the OP is formatted even worse than it was before 🙂

Follow [these instructions](https://discuss.codechef.com/t/tutorial-color-format-the-code/36585) to format your code - the intention is that people should be able to just Copy the code you post and be able to compile it (and read it!) themselves.

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 27, 2020, 2:20pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/6 "2020-08-27T14:20:02Z")

</div>

As has been mentioned on various posts, just clearing sample testcases doesn’t mean your code is correct. Think BIG

There are various edge cases. Think about these and then write the code. Follow a logical process, debug, do dry runs. Think where your code might be failing and try again

Also, three backticks (`) at the beginning and three at the end of the code is gonna format it ‘normally’ I believe you just put those in front of each line of code? idek honestly

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 27, 2020, 2:26pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/7 "2020-08-27T14:26:13Z")

</div>

> [@santosh\_ks15](#):
>
> my code

I understand you’re new here. Please take care while pasting codes/linking submissions. Go through posts telling you about features of CC Discuss and then create a new post. Also, humble suggestion please keep the topic short

The link you have share leads to the submit page, not your submission. Your submission can be linked from

```auto
Profile > problems solved > your submissions > view (your code)

```

or

```auto
Problem page > my submissions option > view code

```

this is [your code](https://www.codechef.com/viewsolution/37185123) (a link to your most recent submission) Please take notice of this

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 27, 2020, 2:29pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/8 "2020-08-27T14:29:50Z")

</div>

As for the question, you could convert said number to a list/string type and then add first and last digit

```auto
Say your number is 1234 (type int)
convert to type list or str
sum = int(string/list index 0) + int(string/list index -1)
so output will be 1 + 4 = 5

```

---

<div class="post-metadata">

**Author:** ![anon11076894](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon11076894](https://discuss.codechef.com/u/anon11076894)\
**Post date:** [August 27, 2020, 5:46pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/10 "2020-08-27T17:46:42Z")

</div>

Be careful in the case where N \< 10. Here the first and last digits are the same and doing this will lead to a wrong answer. Take care of this edge case separately

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 27, 2020, 5:52pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/11 "2020-08-27T17:52:16Z")

</div>

Thank you so much sir  
I am really so much happy that you have responded on my post and sharing your ideas.  
And it’s really helpful for me.I will remember everything from next time that you have told.

Thanks again sir😊

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 27, 2020, 5:54pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/12 "2020-08-27T17:54:47Z")

</div>

After little chance in the code my code successful submitted.

Thanks for providing N\<10 edge case.

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 27, 2020, 5:56pm UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/13 "2020-08-27T17:56:28Z")

</div>

Ok sir  
I will try my best from next time🙂.

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 2:27am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/14 "2020-08-28T02:27:03Z")

</div>

Hey,I added this condition to my original code after I had submitted once, as when N is,say 5, then N[0] and N[-1] will be same, as the first and last index is same(the only index, in this case) ![image](https://s3.amazonaws.com/discourseproduction/original/3X/6/9/69629551b1c6b3d7d22f898b7d2b61c74e386fd6.png).  
So the output will be twice of what is expected i think, but the problem has pretty weak testcases because it gave me AC even without this condition 🤔

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 2:32am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/15 "2020-08-28T02:32:15Z")

</div>

Or maybe that is the answer they wanted? Like for N = 5, expected output could be 10 instead of 5, cause in this case the first and the last digit are both 5 (as seen in above pic) so maybe they were expecting us to add 5 twice…

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 2:33am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/16 "2020-08-28T02:33:44Z")

</div>

Yep. @anon11076894 i submitted the following code and it gave WA:

```auto
T = int(input())
for i in range(T):
	A = input()
	if(int(A) > 9):
		print(int(A[0]) + int(A[-1]))
	else:
		print(int(A[0]))

```

So yeah, the if condition is NOT to be used here @santosh_ks15

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 28, 2020, 2:46am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/17 "2020-08-28T02:46:13Z")

</div>

After changing few statements my code was successful submitted  
[Submitted code](https://www.codechef.com/viewsolution/37185777)

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 2:50am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/18 "2020-08-28T02:50:16Z")

</div>

nice, but this line is still not required…

```auto
if(t>=1 && t<=1000)

```

CONSTRAINTS ARE GIVEN TO GIVE YOU A HINT OF TIME COMPLEXITY. IT IS ‘GUARANTEED’ THAT THE TEST CASES WILL BE WITHIN THESE TEST CASES. YOU DO NOT NEED A CONDITION TO CHECK THIS

---

<div class="post-metadata">

**Author:** ![santosh\_ks15](https://discuss.codechef.com/user_avatar/discuss.codechef.com/santosh_ks15/32/24806_2.png) [@santosh\_ks15](https://discuss.codechef.com/u/santosh_ks15)\
**Post date:** [August 28, 2020, 3:06am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/19 "2020-08-28T03:06:09Z")

</div>

Ok  
Thanks again for all your ideas 😊😊

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 3:07am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/20 "2020-08-28T03:07:56Z")

</div>

you’re very welcome. Keep coding!

---

<div class="post-metadata">

**Author:** ![anon11076894](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon11076894](https://discuss.codechef.com/u/anon11076894)\
**Post date:** [August 28, 2020, 5:06am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/21 "2020-08-28T05:06:45Z")

</div>

@wicked_knight

```auto
t = int(input())
while t > 0:
	n = input()
	list = [int(x) for x in str(n)]
	if len(list) == 1:
		print(n)
	else:
		print(int(n[0]) + int(n[-1]))
	t -= 1

```

here is my [AC code](https://www.codechef.com/viewsolution/26040608) and I have used the condition.

---

<div class="post-metadata">

**Author:** ![wicked\_knight](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wicked_knight/32/112607_2.png) [@wicked\_knight](https://discuss.codechef.com/u/wicked_knight)\
**Post date:** [August 28, 2020, 8:13am UTC](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201/23 "2020-08-28T08:13:08Z")

</div>

why do i get WA for the same code? [CodeChef: Practical coding for everyone](https://www.codechef.com/viewsolution/37194467)

[Next page](https://discuss.codechef.com/t/need-help-my-code-is-successfully-executed-in-code-compile-run-panel-of-codechef-but-on-submission-shows-wrong-answer-the-question-code-is-flow004-first-and-last-digit-of-begineer-level-and-my-code-is/76201.md?page=2)
