odd num, is this right?

closed account (yp9h0pDG)

#include <iostream>
using namespace std;


int main() {
int n, x, m, div, pow=1, i=0;

cin>> n;
x=n;
do{
x/=10;
pow*=10;
}while(x>=10);


div=pow;
do{
m=n/div%10;
div/=10;


if(m%2==1){
i++;

cout<<m;

}
}while(div>=1);
if(i==0){

cout<<"0";

}
Last edited on
It's easier to form the result as a number instead of printing it out one digit at a time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int
main()
{
    int n, digit, result=0, pow = 1;

    cin >> n;

    while (n) {
        digit = n % 10;
        n /= 10;
        if (digit % 2) {
            result += digit * pow;
            pow *= 10;
        }
    }
    cout << result << '\n';
}

Another option is to simply count the number of times you have output a digit (or simply if you have using a boolean), and output a zero if you have not.
Can someone tell me what this means ?

x/=10;
x = x / 10;
Topic archived. No new replies allowed.