sum of palindrome number between a limit

hi i have been doing the problem for beginner section and i not able to solve a program to find sum of all palindromic number between two numbers here is my code and please don’t do any major changed in ti because if you do make a major change then it will not be mine thanks for reading it response immediately needed
and if you see any major mistake then just comment DO IT AGAIN

#include <iostream>
using namespace std;
int main() 
{
 int test,l,r,rev=0,sum=0;
 cin>>test;
 for(int i=0;i<test;i++)
 {  cin>>l>>r;
  rev=0;sum=0;
   for(int j=l;j<=10;j++)
    { rev=rev*10+j%10;
        j=j/10;
      if(rev==j)
      sum=rev+sum;
      } cout<<sum<<endl;
 }
       return 0;
}

Assuming l and r are the smallest and the largest numbers between which you need to find the sum of palindromic numbers, I don’t really get why you do this for(int j=l;j<=10;j++).

should be for(int j=l;j <= r;++j).

Also if rev is your reversed number you are calculating it wrong .
rev would just be the rightmost digit the way you are calculating it .
This is how you should do it .

#include <iostream>
using namespace std;
int main() 
{
 int test,l,r,rev=0,sum=0;
 cin>>test;
 for(int i=0;i<test;i++)
 {
 	cin>>l>>r;
  	sum=0;
   	for(int j=l;j<=r;j++)
    { 
    	rev = 0;//initialise the reversed number to zero for every j 
    	int temp = j;// store j in a temporary variable, we dont want to modify the loop counter variable
    	while(temp>0)
    	{
    		rev = rev*10 + temp%10;
    		temp/=10;
    	}
      	if(rev==j)//if it is a palindrome add it 
      		sum=rev+sum;
    }
    cout << sum << endl;
 }
return 0;
}

Working example

Ask if you don’t understand something !

1 Like

Let’s say we have to find the sum of all palindrome numbers between A and B. We have to check whether i is palindrome number or not where A =< i <= B. To check whether a number is palindrome is not we have to reverse the number and compare it with original number. If both reversed number and original numbers are same then given number is palindrome number and add it the sum of all palindrome numbers.

#include<stdio.h> `
``int main(){

int num,r,sum,temp;

int min,max;

printf(“Enter the minimum range: “);

scanf(“%d”,&min);

printf(“Enter the maximum range: “);

scanf(“%d”,&max);

printf(“Palindrome numbers in given range are: “);

for(num=min;num<=max;num++){

     temp=num;

     sum=0;

     while(temp){

         r=temp%10;

         temp=temp/10;

         sum=sum*10+r;

     }

     if(num==sum)

         printf(“%d “,num);

}

return 0;

}

Hi my code is given below and I get the right answer,but when I submit it returns wrong answer can anybody say what is wrong in it
`
#include<stdio.h>
int palindrome(long long int n)
{
int rI=0,r,oI;
oI=n;
while(n!=0)
{
r=n%10;
rI=rI*10+r;
n=n/10;
}
if(oI==rI)
return 1;
else
return 0;
}
int main()
{

    long long unsigned int l,r,s=0,t;
    int c;
    scanf("%llu",&t);
    while(t>0)
    {
        s=0;
        scanf("%llu%llu",&l,&r);
        while(l<r)
        {   c=palindrome(l);
            if(c==1)
            {
                s=s+l;
            }
            l++;
        }
        printf("%llu\n",s);
        t--;
    }
    return 0;
}`

Please use proper markup syntax for posting code . Reading your code this way is really not easy .