+ 1
Smallest odd digit RECURSIVELY
Hello!I have been strugling with recursion for some time. I have to find the smallest odd digit in a given number with a recursively program. How can i do that? C++ if I may ask you.
7 Respostas
+ 16
Stefan Secrieru
yes , have a look at this Recursion [ all points taken ]
//if something is not clear U can ask ☺👍
import java.util.Scanner;
public class GauravProgram{
public static void main(String[] args){
int a=10,k=oddSmallest(new Scanner(System.in).nextInt(),a);
System.out.println(k==10?-1:k);
}
static int oddSmallest(int n,int a){
if(n/10==0&&n%2==0) return a;
if(n/10==0&&n%2!=0) return n<a?n:a;
else{
if(n%10%2!=0){
a=n%10;n/=10;
return oddSmallest(n,a)<a?oddSmallest(n,a):a;
}
else return oddSmallest(n/10,a);
}
}
}
+ 15
Stefan Secrieru
The program U made will take out smallest digit recursively , not odd smallest digit
//try using condition n%2!=0 for odd mumbers ☺👍
hint : start from last digit, comparing only odd digits & ignoring even digits [if 1st digit odd then move to 2nd last digit for comparision] if 2nd last digit is odd then compare else move to 3rd last digit for comparision & check if odd then compare else move to 4th last digit & so on till number of digits left become 0
+ 11
Stefan Secrieru welcome 😃
//odd-even confusion , wait , I am giving for even also making little change to it [btw logic & idea behind it will be same]
import java.util.Scanner; //for even smallest
public class GauravProgram{
public static void main(String[] args){
int a=10,k=evenSmallest(new Scanner(System.in).nextInt(),a);
System.out.println(k==10?-1:k);
}
static int oddSmallest(int n,int a){
if(n/10==0&&n%2!=0) return a;
if(n/10==0&&n%2==0) return n<a?n:a;
else{
if(n%10%2==0){
a=n%10;n/=10;
return evenSmallest(n,a)<a?evenSmallest(n,a):a;
}
else return evenSmallest(n/10,a);
}
}
}
+ 3
#include <iostream>
int smallestOdd(long, int = 11);
int main() {
long number = 92763387;
std::cout<<smallestOdd(number);
return 0;
}
int smallestOdd(long num, int smallest){
if (num) {
int r = num % 10;
num /= 10;
if (r <= smallest && r % 2) smallest = r;
return smallestOdd(num, smallest);
}
return smallest == 11 ? -1 : smallest;
}
+ 2
https://code.sololearn.com/cHSb7jHT91b1/#cpp
Smallest digit in a number.
+ 1
https://code.sololearn.com/cHSb7jHT91b1/#cpp
If I make it like this then I have 2 problems:
1. I must return -1 if there is not odd digit.
2. if a number starts with 1 it will always return 1.
Can you tell me why? And how to return that -1?
I really apreciate your time.
+ 1
Gaurav Agrawal
Thank you very much for your help. I finally nailed it :)
It took me some time but in the end i had to take a piece of paper and follow your program to realize why i must return n or 10 in the end. You have helped me so much. Thank you!
My C programs looks something like this in the end:(I had to make something there with the b variable to print -1 if there are no EVEN digits. Just don't ask me why i said odd and not even XD)
int n,b=0;
int cifminpar(int n)
{
if(n>=10){
if(n%10%2==0){
b=1;
return n%10<cifminpar(n/10)?n%10:cifminpar(n/10);
}
else
return cifminpar(n/10);
}
else
if(n%2==0){
b=1;
return n;
}
else
if(b==0)
return -1;
else
return 10;
}
Gaurav Agrawal