Program to print all palindromes in a given range in C++

C++Server Side ProgrammingProgramming

<p>In this tutorial, we will be discussing a program to print all palindromes in a given range.</p><p>For this we will be given the mathematical range in which the palindromes are to be found. Our task is to find all the palindromes in that range and print it back.</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/oCa8CNXJ" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include&lt;iostream&gt; using namespace std; //checking if the number is a palindrome int is_palin(int n){ &nbsp; &nbsp;int rev = 0; &nbsp; &nbsp;for (int i = n; i &gt; 0; i /= 10) &nbsp; &nbsp;rev = rev*10 + i%10; &nbsp; &nbsp;return (n==rev); } void countPal(int min, int max){ &nbsp; &nbsp;for (int i = min; i &lt;= max; i++) &nbsp; &nbsp;if (is_palin(i)) &nbsp; &nbsp;cout &lt;&lt; i &lt;&lt; &quot; &quot;; } int main(){ &nbsp; &nbsp;countPal(99, 250); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate">99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242</pre>
raja
Updated on 19-Dec-2019 10:36:25

Advertisements