Palindrome numbers between 1 to 1000 – CSKC
Sign In

Search

Solved Problem #CPP#1553    siteicon   siteicon  

Problem Statement - Palindrome numbers between 1 to 1000

Print all palindrome numbers between 1 to 1000.

Solution

TC++ #1553

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

Notes

for(int n=1;n<=1000;++n) {
This is external loop to test every number from from 1 to 1000

int rem,nr=0;  //rem initialised to collect remainder and nr is to get the reversed number
int nc=n; //nc is to create copy of number so that it can be used for reseversal loop. This allowed to later compare n with nr for palindrome testing. If we do not run loop on nc, the value of n will become 0 due to very nature of reversal loop.

while(nc>0) {
This is internal loop to reverse the integer number n which is now save in nc

rem=nc%10; //Last digit is extracted which is remainder after division from 10
nr=nr*10+rem; //Previous number multiplied by 10 to shift it left and then added with Last digit. This will successive allow number reversal.
nc=nc/10; } //Number is reduced by cutting the last digit so that whole number is processed digit by digit starting from the rightmost digit.

if(n==nr) cout<<n<<” “; //This tests if reversed number is same as original number and when same number is understood as a palindrome number so it is printed.
}


Share

Created: 2-Dec-2017 11:22 | Updated: 14-Dec-2018 00:16|