Sunday, September 11, 2011

number program





/*
Definition from Aniruddha Guddi:
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
*/


/**************************
* author: umang bhatt *
* bhatt.umang7@gmail.com *
**************************/


#include<stdio.h>

#define TRUE 0
#define FALSE 1


int
contains_same_digits(int no1, int no2)
{

int
arr1[10] = {0};

int
arr1_top = -1;
int
arr2[10] = {0};

int
arr2_top = -1;
int
temp = no1;

int
i, j, k;
int
returnval;

int
flag = FALSE;

//store numbers in array
while (temp > 0)
{


arr1[++arr1_top] = temp % 10;
temp = temp / 10;
}


temp = no2;
while
(temp > 0)
{


arr2[++arr2_top] = temp % 10;
temp = temp / 10;
}






for
(i = 0; i <= arr1_top; i++)
{


flag = FALSE;
for
(j = 0; j <= arr2_top; j++)
{


if
(arr1[i] == arr2[j])
{

flag = TRUE;

// remove that element from both arrays
for (k = i; k < arr1_top; k++)
{


arr1[k] = arr1[k + 1];
}

arr1_top--;

for
(k = j; k < arr2_top; k++)
{


arr2[k] = arr2[k + 1];
}

arr2_top--;


if
(j != 0)
j--;
}
}


if
(flag == TRUE)
{

i = -1;
}
}


if
(arr1_top == -1 && arr2_top == -1)
{

returnval = TRUE;
}


else

{

returnval = FALSE;
}

return
returnval;
}


int
main()
{

char
c[5];
int
i, j;

int
flag;
int
flag1;

for
(j = 2; j <= 6; j++)
{


i = 1;
flag = FALSE;
while
(flag == FALSE)
{


flag = contains_same_digits(i, (i * j));
if
(flag == TRUE)
{


printf("\n %d * %d = %d where digits of %d and %d are same ", i, j, (i * j), i, (i * j));

flag = TRUE;
}

i++;
}
}

printf("\n");

return
0;
}




2 comments: