Skip to content

Instantly share code, notes, and snippets.

@Sanokei
Created November 3, 2021 03:13
Show Gist options
  • Save Sanokei/2594a4b5af5ea3016249bd95f01648d6 to your computer and use it in GitHub Desktop.
Save Sanokei/2594a4b5af5ea3016249bd95f01648d6 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
//using a recursive function get the first even digit in the number from all the individual digits in the number
int firstEven(int num){
if(num < 10){
if(num % 2 == 0){
return num;
}
else{
return 0;
}
}
else{
if(num % 2 == 0){
return num % 10;
}
else{
return firstEven(num/10);
}
}
}
int main(){
int num;
cout << "Enter a number: ";
cin >> num;
cout << "The first even digit in the number is: " << firstEven(num) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment