-4

I want to compare the size of two numbers and print the larger value in reverse. but, the output value is different from what I expected.

#include<iostream>
int main() {
    int a, b;
    int big;
    std::cin >> a>>b;
    if (a > b)
        big = a;
    if (a <= b)
        big = b;
    std::cout << "input" << a <<" "<< b<<"\n";
    std::cout << "bigger"<<big << "\n";
    
    int q, w, e;
    e = big % 10;
    w = (big % 100)-e;
    q= big - (e)-(w * 10);
    std::cout << e << w << q;
    return 0;
}
  1. This code receives two integers from the user and stores them in variables a and b.
  2. It compare two values and store the larger value in the variable called “bigger” by using if statement.
  3. Store the remainder of division by 10 in q
    The remainder of division by 100 minus q is stored in w
    Subtract q and 10 times w from the value stored in 'bigger' and store the result in 'e'.
  4. Output value of e,w,q without spaces.

there is some example that I expected. input 123 456 output 123 456 input123456 bigger456 654

but this is the result. 123 456 input123 456 bigger456 650-50

I searched this question. this question It shows How to print a number backwards with a built-in function. So I asked new question.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
urie1
  • 1
  • 2
  • I think you need to divide `w` by ten. You should use more meaningful variable names, especially if you want others (or even yourself) to understand your code – Alan Birtles Apr 03 '23 at 10:43
  • Learn about std::vector + std::reverse, or reverse iterators (or reverse view C++20). – Pepijn Kramer Apr 03 '23 at 10:51
  • You're more likely to succeed if you follow the naming given by the instructions. – molbdnilo Apr 03 '23 at 10:58
  • Does this answer your question? [I simply write a program to reverse a number but the output not meet my expectation](https://stackoverflow.com/questions/74404280/i-simply-write-a-program-to-reverse-a-number-but-the-output-not-meet-my-expectat) – sweenish Apr 03 '23 at 11:12
  • My dupe is not a perfect match, but my answer to the question provides three working samples to reverse a number. – sweenish Apr 03 '23 at 11:13

2 Answers2

1

You can use std::to_string to convert a number to a string.

Then std::reverse to reverse the string.

#include <algorithm>
#include <string>

...

std::string big_s = std::to_string(big);
std::reverse(big_s.begin(), big_s.end());
std::cout << big_s;
anatolyg
  • 26,506
  • 9
  • 60
  • 134
-2

there is a problem with the calculation of w. In the current code, w is calculated as (big % 100) - e, which only works if e is always a single digit. However, if e is a two-digit number (i.e., if the last two digits of big are the same), then w will be negative. To fix this issue, you can modify the calculation of w to extract the last two digits of big and subtract e from them.


try this

#include<iostream>
using namespace std;
int main() {
    int a, b;
    int big;
    std::cin >> a >> b;
    if (a > b)
        big = a;
    if (a <= b)
        big = b;
    std::cout << "input: " << a << " " << b << "\n";
    std::cout << "bigger: " << big << "\n";

    int q, w, e;
    e = big % 10;
    w = (big / 10) % 10; // Extract the second-to-last digit of big
    q = big / 100; // Extract the first digit of big
    std::cout << e << w << q << endl;
    return 0;
}