0
How to include zero in a reversed number loop
Alternative way https://code.sololearn.com/cV31ygXumu6Q/?ref=app
5 Respostas
+ 4
Include zero prefix like 0123? not possible if you use number. Possible if you work with string instead.
+ 2
No problem 😁👌
+ 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;
}
+ 1
Ipang thank you for clearing this up!
+ 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