![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
#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
Advertisements