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

C++Server Side ProgrammingProgramming

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
raja
Published on 16-Jan-2021 07:43:03
Advertisements