Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<math.h>
int numdig(int x)
{
    int i=0;
    for(i=0;i<100;i++)
    {
        x=x/10;
        if(x==0)
            break;
    }
return (i+1);
}
void sort(int a[],int n)
{
    int i=0,j,temp;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
               temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
}
int numcheck(int x)
{   int count=0;
    int i=0,j,y;
    int a[50],b[50];
    for(i=0;i<50;i++)
    {
        a[i]=x%10;
        x=x/10;
        if(x==0) break;
    }
    j=i; y=2*x;
     for(i=0;i<50;i++)
    {
        b[i]=y%10;
        y=y/10;
    }
    sort(a,j+1);sort(b,j+1);
    for(i=0;i<j;i++)
    {
        if(a[i]==b[i]) count++;
    }
    if(count==j+1) return 1;
    else return 0;
}
int main()
{
  long long int a,b,i; int t,k;
    printf("enter the number of cases\n");
    scanf("%d",&t);

    for(k=0;k<t;k++)
    {
printf("enter the number which are upper bound and lower bound\n");

    scanf("%d",&a);
    scanf("%d",&b);
    for(i=a;i<b;i++)
    {
        if(numdig(i)!=numdig(2i))
            break;
        else if (numcheck(i)==1)
        printf("this is one of the required number : %d",i);
        else continue;
    }
    }
    return 0;

}
Posted
Updated 9-May-15 4:05am
v3
Comments
Richard MacCutchan 9-May-15 8:47am    
You need to provide more details. What is this supposed to do, what does it actually do and why is that wrong? And please indicate where the error occurs.
Member 11324568 9-May-15 8:50am    
first input which i defined as number of cases is actually the number of different ranges between which you want to check.And the other two inputs are the ranges for each of the case.I need to find a number x such that x and 2x are permutations of eachother.I need to print all such x between the different sets of ranges in each of the cases.Thanks...there is no error but any nummber isn't being printed..so i am confused if my code is logically incorrect or really there is no such number between the ranges i input...thanks
Richard MacCutchan 9-May-15 8:54am    
I am afraid that does not help. You need to use your debugger, or add some extra print commands, to see what is happening at different stages of the program.
CPallini 9-May-15 9:20am    
What does it mean: "such that x and 2x are palindromes of each other"? Can you please give us an example?
Member 11324568 9-May-15 10:07am    
i'm sorry @cpallini its permutation not palindrome

Start by thinking about what a Palindrome is: it's a word or phrase that when reversed is the same as it was to start with: "TACO CAT" for example.
So what you are looking for is a number like "12345" that when doubled equals it's own reverse: "54321" (clearly, that one doesn't work).

So start by writing a function to return the palindrome of a number: pass it an integer (12345, say) and it returns it's reverse (54321 this time). You can then use that to compare and see if it's the same number.

Since this is your homework, I won't give you the code. But I'll give you a clue or two:
1) The modulus operator "%" will extract the least significant decimal digit if you use 10.
2) The integer divide operator "/" will "throw away" the least significant digit if you use 10.
3) Looping until a value is zero is really, really, easy...

Give it a try: if you meet a problem and you can't solve it, feel free to ask about it.
 
Share this answer
 
Comments
Member 11324568 9-May-15 10:06am    
i'm extremely sorry sir.It is actually permutation i got confused with the two words..Its not a homework sir,i took this problem from codechef.com/hardquestions section
Member 11324568 9-May-15 10:09am    
what i did is i extracted all the digits of the number x and 2x into two different arrays and then sorted those two arrays and checked for the equality of the two arrays...for it i initially created function which returns the number of digits ,if they are equal then i checked furthur by checking the digits else i broke the loop..
OriginalGriff 9-May-15 10:14am    
I think you need to read the question really carefully - permutation is something else again:
"In mathematics, the notion of permutation relates to the act of rearranging, or permuting, all the members of a set into some sequence or order (unlike combinations, which are selections of some members of the set where order is disregarded)." -- Thanks Google!

If what you mean is that you are looking for a number that when doubled has exactly the same digits (but in a different order) then I'd cheat: Sort the digits of each number and compare the sorted values...
Your ideas are good, however the implementation is not so good.
The following code ( please note: I don't claim it is correct :-) ) produces
found 125874 (251748)

as first of such numbers.

C
#include<stdio.h>

void sort(int a[],int n)
{
  int i, j, temp;

  for(i=0;i<n-1;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if ( a[i] > a[j] )
      {
        temp = a[i];
        a[i] = a[j];
        a[j]=temp;
      }
    }
  }
}

int make_array(int x, int a[], int size)
{
  int i;

  for (i = 0; i < size; ++i)
  {
    a[i] = x % 10;
    x /= 10;
    if ( x == 0 ) break;
  }
  return (i +1);
}

int numcheck(int x)
{
  int i;
  int sa, sa2;
  int a[50], a2[50];

  sa  =  make_array( x, a, 50);
  sa2 =  make_array( x * 2, a2, 50);
  if ( sa != sa2) return 0;

  sort( a, sa);
  sort( a2, sa);

  for(i=0; i<sa; i++)
  {
    if ( a[i] != a2[i] ) break;
  }
  if (i != sa) return 0;
  return 1;
}

int main()
{
  int a,b,i; int t,k;
  printf("enter the number of cases\n");
  scanf("%d",&t);

  for(k=0;k<t;k++)
  {
    printf("enter the number which are upper bound and lower bound\n");

    scanf("%d",&a);
    scanf("%d",&b);
    for(i=a;i<b;i++)
    {
      if ( numcheck( i ) == 1)  printf("found %d (%d) \n", i, 2*i);
    }
  }
  return 0;
}
 
Share this answer
 
Comments
Member 11324568 9-May-15 11:42am    
that was good and it is 100% correct ,thanks a lot :-)
CPallini 9-May-15 11:46am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900