+ 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.
8 Antworten
+ 6
So try to develop it by yourself. If you have a question, link your code and ask it here.
+ 4
Attempts??
+ 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.
+ 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.
+ 2
can you share with us your attempt
+ 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.
+ 1
Try the below code with some other sample inputs,
https://code.sololearn.com/cecmeKCG44Ac/?ref=app
+ 1
Thank You very much Kabilan K