Code editing

So this code should print the reverse of any number user enters
example: 123
output: 321
but in line "16" can I remove cout << "-"; but still print the number with the negative value .

Note: I know how to code it without array, but array is needed..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;
int i = 0;
int answer;
int arr[100000];
void rev(int x);

int main() {
    int x;
    cin >> x;
    if ( x >= 0 ){
        rev (x);
    }
    else {
        x = (x*-1);
        cout << "-";
         rev (x);
        
    }

    return 0;
}
void rev (int x){
    do {
         arr[i] = x % 10;
        i++;
        x = x / 10;
    }while (x != 0);
    for ( int j = 0; j < i; j++){
        cout <<  arr[j];
    }
}
closed account (SECMoG1T)
Yeah the negative sign is always added by
x = (x*-1);// line 15
You didn't get what I mean
You can either change the sign of the first element of arr that is being printed out (multiply it by negative 1), or leave the cout << "-" in there. Unless you don't need it to be negative. If you just want the reverse of the number, then you can remove that cout statement and it'll just print it in reverse (i.e. print "54321" when the user enters "12345"). If you want it to be negative, then you have to either change the sign of the first element being outputted or insert the "-" sign somewhere (it'll print "-54321" when the user enters "12345").
Topic archived. No new replies allowed.