Demlo number, Square of 11...1 in C++ Program


In this tutorial, we are going to learn about the demlo number.

The demlo numbers are the squares of the number 1, 11, 111, 1111, etc.., We can easily find the demlo number as it is in the form 1 2 3 4 5 ... n-2 n-1 n n-1 n-2 ... 5 4 3 2 1.

Here, we are given a number which has only ones. And we need to find the demlo number of that number. Let's see an example.

Input − 1111111

Output − 1234567654321

Let's see the steps to solve the problem.

  • Initialize the number in a string format.

  • Initialize an empty string to store the demlo number.

  • Iterate from 1 to the length of the number n.

  • Add all the numbers to the demlo number.

  • Now, iterate from n - 1 to 1.

  • Add all the numbers to the demlo number.

  • Print the demlo number.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
string getDemloNumber(string str) {
   int len = str.length();
   string demloNumber = "";
   for (int i = 1; i <= len; i++) {
      demloNumber += char(i + '0');
   }
   for (int i = len - 1; i >= 1; i--) {
      demloNumber += char(i + '0');
   }
   return demloNumber;
}
int main() {
   string str = "1111111";
   cout << getDemloNumber(str) << endl;
   return 0;
}

Output

If you run the above code, then you will get the following result.

1234567654321

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 27-Jan-2021

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements