inputs an integer and displays the first 2-digits present in it

Problem Statement: A program that inputs an integer and displays the first two- digits number present in it

Sample Input:
1) 341531
2) 95402
3) 59
Sample Output:
1) 34
2) 95
3) 59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int first2_digit(vector<vector<int>> number, int win)
{
    int first_digit;
    if ( number.size() % 2 == 0) // number of digit is even
    {
    do {
        first_digit = win % 100;
        win /= 100;
    } while (win > 0);
    }
    else{ // number of digit is odd
        do {
            first_digit = win % 1000;
            win /= 100000;
        } while (win > 0);
    }
    return first_digit;
}


My code has problem with odd number of digit
For example: if the input number is 12345, the output will be 1 instead of 12
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    std::cout << "enter an integer: " ;
    int n ;
    std::cin >> n ;

    if( n < 0 ) n = -n ; // ignore the sign

    // keep chopping off the rightmost digit, till only two digits remain
    while( n > 99 ) n /= 10 ;

    std::cout << n << '\n' ; // print the result
}
Topic archived. No new replies allowed.