Demlo number (Square of 11...1)” in C++?


Demlo numbers are palindromic numbers that are generated by the square of number of form 11..1 given that the number is less than 10 digits.

Let us first declare the string variables −

string demNum = "1111";
string square = "";

Now, we loop till the length of the demNum string. Inside the loop we convert the index value i to string and append it to square variable.

for(int i=1 ;i<=demNum.length();i++){
   square += char(i+'0');
}

In the second loop we loop in reverse starting from the length of demNum string. Inside the loop we convert the index value i to string and append it to square variable.

for (int i = demNum.length() - 1; i >= 1; i--)
   square += char(i + '0');

Example

Let us see the following implementation to get a better understanding of demlo numbers −

 Live Demo

#include <iostream>
using namespace std;
int main(){
   string demNum = "1111";
   string square = "";
   for(int i=1 ;i</=demNum.length();i++){
      square += char(i+'0');
   }
   for (int i = demNum.length() - 1; i >= 1; i--)
      square += char(i + '0');
   cout << square;
   return 0;
}

Output

The above code will produce the following output −

1234321

Updated on: 16-Jan-2021

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements