# c programing help

This is a discussion on c programing help within the C Programming forums, part of the General Programming Boards category; The Palindromes A string is said to be palindrome, if it reads same from both ends, i.e, from left-to-right and ...

1. ## c programing help

The Palindromes
A string is said to be palindrome, if it reads same from both ends, i.e, from left-to-right and from
right-to-left. For example, “MALAYALAM” is a palindrome string. The letters are symmetrical at
both ends.
Similarly, a numeral palindrome has corresponding digits from the both ends symmetrical. So, on
reversing the digits of the number, we get the same number itself. Thus, 23432 is a palindrome
number. Further, some numbers have the property that they are palindrome in both decimal, and
binary bases. One such example is the number 58510 = 10010010012
We see that both 585 and 1001001001 are palindromes.
In a given range, you have to find out those numbers which are palindrome in both decimal and
binary bases, and report their decimal sum.
Input
The first line of the input will have a single integer t (1 <= t <= 50), the number of test cases. Then,
t test cases will follow. Each test case will consist of two numbers m and n, which will define the
lower limit and the upper limit of the range (m, n <= 10000000 and m < n).
Output
The output will contain t lines with exactly one number on each line, the sum of all the palindromes
(in both decimal and binary bases) that lie in the given range [m, n].

3. Code:
```#include<conio.h>
#include<stdio.h>
#include<math.h>
void paliendrome(int a,int b)
{
int m;
for (i=a;i<=b;i++)
{
m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
}
}
int  convertdecimaltobinary(int x)
}
void checkpaliendrome(int y)
{
int k=y,new=0;
int r=y;
int u,v,c=0;//c counts the number of digits
while(k>0)
{
u=k%10;
k=k/10;
c=c+1;
}
while (r>0)
{
u=r%10;
v=pow(10,c-1)*u ;
new=new+v;
c--;
}
if (new ==y)
{ printf("%d",y);
}
}

//both decimal and binary equivalents have to be paliendromes

void main()
{
int t,u,l;
scanf("%d",&t);
int i ;
//t stands for the no of cases
for(i=0;i<=t;i++)
{
scanf("upperrange",&u);
scanf("lowerrange",&l);
paliendrome(u,l);
}```

4. This problem is quite simple . . .

Assuming you're working a maximum size of, say, 32. (unsigned long int, in other words)

(yes, I *know* that long is the same thing as int on most modern systems.)
Code:
```char *convert_to_binary(unsigned long number) {
int x;
char *result = malloc(sizeof(char) * 33);
for(x = 1; x < 32; x ++) {
}
for(x = 0; x < 32; x ++) {
}
return result;
}```
Not that there is insufficient error checking etc. in there.

Also, I haven't tried it, so I'm not 100% certain if it'll work, but that should work.

5. Code:
```there is one error in your code ---  type mismatch in redeclaration of checkpaliendrome

#include<conio.h>
#include<stdio.h>
#include<math.h>
void paliendrome(int a,int b)
{
int m;
for (i=a;i<=b;i++)
{
m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
}
}
int  convertdecimaltobinary(int x)
{
int x;
char *result = malloc(sizeof(char) * 33);
for(x = 1; x < 32; x ++) {
}
for(x = 0; x < 32; x ++) {
}
return result;
}

void checkpaliendrome(int y)
{
int k=y,new=0;
int r=y;
int u,v,c=0;//c counts the number of digits
while(k>0)
{
u=k%10;
k=k/10;
c=c+1;
}
while (r>0)
{
u=r%10;
v=pow(10,c-1)*u ;
new=new+v;
c--;
}
if (new ==y)
{ printf("%d",y);
}
}

//both decimal and binary equivalents have to be paliendromes

void main()
{
int t,u,l;
scanf("%d",&t);
int i ;
//t stands for the no of cases
for(i=0;i<=t;i++)
{
scanf("upperrange",&u);
scanf("lowerrange",&l);
paliendrome(u,l);
}

please tell me how will correct it```

6. i have added indentetion to the code and some questions in the comments
Code:
```//conio.h is not standard
#include<conio.h>
#include<stdio.h>
#include<math.h>

void paliendrome(int a,int b)
{
int m;
for (i=a;i<=b;i++)
{
m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
}
}
int  convertdecimaltobinary(int x)
{
int x; /* this local var hides the past parameter */
/* do you see the type of result? */
char *result = malloc(sizeof(char) * 33);
for(x = 1; x < 32; x ++) {
bitmasks[x] = bitmasks[x-1] * 2; /* what do you think this is doing? */
}
for(x = 0; x < 32; x ++) {
}
return result; /* you trying to return char* while return type is int */
}
/* are you checking binary or decimal palindrom? */
void checkpaliendrome(int y)
{
int k=y,new=0;
int r=y;
int u,v,c=0;//c counts the number of digits
while(k>0)
{
u=k%10;
k=k/10;
c=c+1;
}
while (r>0)
{
u=r%10;
v=pow(10,c-1)*u ;
new=new+v;
c--;
}
if (new ==y)
{ printf("%d",y);
}
}

//both decimal and binary equivalents have to be paliendromes

/* main should be int main(void) - read FAQ */
void main()
{
int t,u,l;
scanf("%d",&t);
int i ;
//t stands for the no of cases
for(i=0;i<=t;i++)
{
scanf("upperrange",&u); /* you have no format specifier here */
scanf("lowerrange",&l);
paliendrome(u,l);
}
}```