Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello! 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.

What I have tried:

C++
#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;
    }
  }
}
Posted
Updated 28-Sep-21 12:18pm
v5
Comments
jeron1 28-Sep-21 19:09pm    
Not sure why the question was downvoted, a question was asked, the input, expected output, actual output, and code attempt was provided. A little harsh I think.

1 solution

Try using the stream formatting operators:
C++
#include <iostream>
#include <iomanip>

using namespace std;

int main()
    {
    int i = 1;
    int j = 17;
    cout << std::setw(2) << std::setfill('0') << i << "," << j << std::endl;
    return 0;
    }
 
Share this answer
 
Comments
CPallini 28-Sep-21 2:57am    
5.
merano99 28-Sep-21 18:10pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900