Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a task to write a program in C ++ with the following condition: To write a program for obtaining all natural numbers not exceeding a given n, which represent a strictly increasing sequence of digits. I will be very grateful if you help me with the code.

What I have tried:

#include <iostream>
int main()
{
	using namespace std;

	int n;
	cin >> n;

	for (int i = 1; i <= n; i++)
	{
	}

	return 0;
}
Posted
Updated 23-Nov-21 0:52am
Comments
Richard MacCutchan 19-Nov-21 7:09am    
That is just a simple matter of counting from 1 to n.

This function:
C++
#include <iostream>
bool isStrictlyIncreasing(int x) {
    // check consecutive digits, from right to left:
    // start by checking last digits
    int lastDigit = x % 10;
    int leadingDigits = x / 10;
    while (leadingDigits > 0) {
        int digit = leadingDigits % 10; // digit before lastDigit
        if (digit >= lastDigit)
            return false;
        // move test window one digit to the left
        lastDigit = digit;
        leadingDigits = leadingDigits / 10;
    }
    return true;
}
int main()
{
    int tests[] {2, 11, 13, 20, 123, 120, 1243, 2334};
    for (int i = 0; i < 8; ++i) {
        std::cout<< tests[i] << " is increasing: " << isStrictlyIncreasing(tests[i]) << "\n";
    }
    char c;
    std::cin >> c;

    return 0;
}

returns this output:
2 is increasing: 1
11 is increasing: 0
13 is increasing: 1
20 is increasing: 0
123 is increasing: 1
120 is increasing: 0
1243 is increasing: 0
2334 is increasing: 0

Is that what you were looking for?
 
Share this answer
 
We are more than willing to help anyone who is genuinely stuck, but we will not write your homework assignment for you and you have made no effort to solve this for yourself.

Here is a site that will give you the definition of natural numbers and how to find them Natural Numbers - Concepts, Properties, Number Line & Examples[^]
Here is an article that should help you get started on a solution How to Write Code to Solve a Problem, A Beginner's Guide[^]

Start coding, give it a go and if you get stuck then please do come back here with your code, explain your problem and we will try to help
 
Share this answer
 
Quote:
but I can't do "which is a strictly ascending sequence of numbers"

The key is to split the number into digits.
C++
int number=123;
int unit= number % 10; // unit= 3
int rest= number / 10; // rest= 12
 
Share this answer
 
By construction:
C++
#include <iostream>
using namespace std;

void add_dgt(int num, int dgt, int limit)
{
  num *= 10;
  for (; dgt < 10; ++dgt)
  {
    if (( num + dgt ) <= limit)
      cout << (num + dgt) << endl;

    if ( dgt < 9 )
    {
      add_dgt( num + dgt, dgt + 1, limit);
    }
  }
}

int main()
{
  int limit;
  cout << "please enter the limit ";
  cin >> limit;

  if (limit > 0 )
    add_dgt(0, 1, limit);
}
 
Share this answer
 
As you can see above I've made the code somewhere, but I can't do "which is a strictly ascending sequence of numbers"

#include <iostream>
int main()
{
	using namespace std;

    bool isStrictlyIncreasing( int x )
	int n;
	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		if ( isStrictlyIncreasing( i ) ) cout << i << '\n';
	}

	return 0;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 19-Nov-21 8:48am    
I have no idea what that is supposed to be, but as I already suggested, all you need is a simple loop that counts from 1 to n.
Stefan_Lang 22-Nov-21 11:00am    
The OP says "natural numbers that have a strictly increasing sequence of digits". So 1, 2, 3, and 12 would be in, but 10 and 11 would be out because their digits are not increasing.
Richard MacCutchan 22-Nov-21 11:23am    
I don't understand where the out of sequence numbers come from.
Stefan_Lang 22-Nov-21 12:09pm    
The number 10 has digits 1 and 0, so the digits are decreasing.
The number 12 has digits 1 and 2, so the digits are strictly increasing.

12 fulfils the criterium, 10 doesn't.
Richard MacCutchan 22-Nov-21 12:37pm    
Thanks, I misunderstood the question, despite reading it a number of times.

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