Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello! How can I have an output of
Sample Input:
9
Sample Output:
1.2.3.4.5.6.7.8.9

If you input numbers below or equal to 9, the output should be (1.2.3.4.5.6.7.8.9)

But if you input a number greater than 9, for example:
Sample Input No.2:
20
Sample Output No.2:
01.02.03.04.05.06.07.08.09.10
11.12.13.14.15.16.17.18.19.20

My code below is for Sample Input & Output No.2. I tried adding another for loop but it still reads Sample Input & Output No.2 code. What should I do?

What I have tried:

C++
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	int a, num;
	cin >> num;
	
	if (num > 100 || num <= 1){
	
	cout << "OUT OF RANGE";
}
	else {
		for (int a = 1; a < num; a++){
			cout << setfill('0') << setw(2) << a  << ".";
	
	}
	cout << num;
	}
	
}
Posted
Updated 29-Sep-21 11:32am

The problem is with the output manipulators. Look there. Experiment.

<iomanip> - C++ Reference[^]
 
Share this answer
 
v2
Quote:
What should I do to remove preceding '0' for numbers below or equal to 9?

Yesterday you ask how to enforce a leading '0' for values below 10, and you got the code you are using now.
No leading '0' is just default behavior.
Try to replace
C++
cout << setfill('0') << setw(2) << a  << ".";

with
C++
cout << a  << ".";
 
Share this answer
 

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