0

How to include zero in a reversed number loop

Alternative way https://code.sololearn.com/cV31ygXumu6Q/?ref=app

21st Mar 2021, 5:02 AM
Jessica Zarate
Jessica Zarate - avatar
5 Respostas
+ 4
Include zero prefix like 0123? not possible if you use number. Possible if you work with string instead.
21st Mar 2021, 5:08 AM
Ipang
+ 2
No problem 😁👌
21st Mar 2021, 5:31 AM
Ipang
+ 2
zero to the left is not valid, but to show, check if reversed number is zero, and print: #include <iostream> using namespace std; int main() { int n, reversedNumber = 0, remainder; cout << "Enter an integer: "; cin >> n; cout<<n<<endl; cout<<"Reversed Number = "; while(n != 0) { remainder = n%10; reversedNumber = (reversedNumber * 10) + remainder; if(reversedNumber == 0) cout<< 0; n /= 10; } cout << reversedNumber; return 0; }
23rd Mar 2021, 3:27 AM
Ruben Enrique
Ruben Enrique - avatar
+ 1
Ipang thank you for clearing this up!
21st Mar 2021, 5:22 AM
Jessica Zarate
Jessica Zarate - avatar
+ 1
You can use iomanip (#include <iomanip>) to: a) Set the amount of digits to output b) Set a filler Then you just need to count the digits and put " << setw(digits) << setfill('0')" in front of the output. Here is an example of how to use it: https://code.sololearn.com/cpD3b6c7xls4/?ref=app
21st Mar 2021, 10:50 AM
Alex
Alex - avatar