I need a C program to print the digit immediately after the last even digit of a number. e.g input 23613 output 1. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I need a C program to print the digit immediately after the last even digit of a number. e.g input 23613 output 1.

#include <stdio.h> main() { int x,d,y; printf("enter the integer x="); scanf("%d",&x); while(x>0) { d=x%10; x=x/10; if(d%2==0) { y=x%10; printf("%d",y); break;} } } But it finds the digit immediately before the last even digit. I need the digit immediately after the last even digit of the number. Please help me.

22nd Apr 2023, 3:02 PM
Debjit Dey Sarkar
Debjit Dey Sarkar - avatar
8 Answers
+ 6
So try to develop it by yourself. If you have a question, link your code and ask it here.
22nd Apr 2023, 4:41 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Attempts??
22nd Apr 2023, 3:21 PM
Hasnain [ACTIVE CHALLENGER]
Hasnain [ACTIVE CHALLENGER] - avatar
+ 4
Before we start, some side hints that will help in the future: 1. Use meaningful variable and function names. They help understanding the code's logic. 2. Instead of copying and pasting the code, add in the question description a link to your code in Code Playground. That allows people to run and debug your code, and test possible solutions. And avoid copy errors. Now on the code: when you find an even digit, you output y, which equals (x/10)%10, which is indeed the digit immediately before. A possible solution would be to reverse the logic behind d and y calculations, and make y be after d, not before.
23rd Apr 2023, 12:12 PM
Emerson Prado
Emerson Prado - avatar
+ 3
Kabilan K Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. Prefer giving hints for the OP to find the solution instead.
24th Apr 2023, 9:04 AM
Emerson Prado
Emerson Prado - avatar
+ 2
can you share with us your attempt
22nd Apr 2023, 3:22 PM
Amine Laaboudi
Amine Laaboudi - avatar
+ 1
#include <stdio.h> main() { int x,d,y; printf("enter the integer x="); scanf("%d",&x); while(x>0) { d=x%10; x=x/10; if(d%2==0) { y=x%10; printf("%d",y); break;} } } It was my attempt but it find the digit immediately before the last even digit. But i need the digit immediately after the last even digit.
23rd Apr 2023, 6:18 AM
Debjit Dey Sarkar
Debjit Dey Sarkar - avatar
+ 1
Try the below code with some other sample inputs, https://code.sololearn.com/cecmeKCG44Ac/?ref=app
24th Apr 2023, 4:20 AM
Kabilan K
Kabilan K - avatar
+ 1
Thank You very much Kabilan K
24th Apr 2023, 3:41 PM
Debjit Dey Sarkar
Debjit Dey Sarkar - avatar