1

How can I remove the preceding '0' from number 10 above? Only numbers 1-9 should have preceding '0'.

SAMPLE INPUT: 40

SAMPLE OUTPUT: 01.02.03.04.05.06.07.08.09.10 11.12.13.14.15.16.17.18.19.20 21.22.23.24.25.26.27.28.29.30 31.32.33.34.35.36.37.38.39.40

MY CODE'S OUTPUT: 01.02.03.04.05.06.07.08.09.010 011.012.013.014.015.016.017.018.019.020 021.022.023.024.025.026.027.028.029.030 031.032.033.034.035.036.037.038.039.040

Can you help me, please? I tried different ways but still won't work. I'm new in programming btw, that's why I don't know much.

Here's my code:

#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> b;
if (b > 100){

cout << "OUT OF RANGE";
}
else {

for (int a = 1; a <= b; a++){
cout << "." << "0" << a;

}
}
}
Hyuyeong
  • 35
  • 5
  • *"How can I remove the preceding '0'"* -- since you are the one who added the zero (with the `"0"` in the line `cout << "." << "0" << a;`, it might be better to view this question as *"How can I avoid adding the `0` when the number is 10 or above?"* This might lead you to wonder how you could end your loop when the number is `>= 10`, even if it is still `<= b`... which might be useful if there was a second loop, much like the current one but without the `"0"`... – JaMiT Sep 28 '21 at 03:23

1 Answers1

2
std::cout <<"." << std::setfill('0') << std::setw(2) << a;
bolov
  • 72,283
  • 15
  • 145
  • 224
kalyanswaroop
  • 403
  • 4
  • 5
  • 1
    Or, if you dont want to use all that, you can also use an if(a<10) statement and do a cout with the "0" and in the else part, do a cout without the "0" – kalyanswaroop Sep 28 '21 at 03:10