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;
}




C program : largest palindrome made by multiplication of two 3 digit numbers





/*
Definition from Aniruddha Guddi:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/


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





#include<stdio.h>

#define TRUE 0
#define FALSE 1

int
is_palindrome(int no)
{

int
temp = no;

int
ans = 0;
int
r;
while
(temp > 0)
{


ans = ((ans * 10) + (temp % 10));

temp = temp / 10;
}

if
(ans == no)
{


r = TRUE;
}

else

{

r = FALSE;
}


return
r;
}


int
main()
{

int
f_no = -1;

int
l_no = -1;
int
last_palindrom = -1;

int
flag = FALSE;
int
i, j;

for
(i = 100; i < 999; i++)
{


for
(j = 100; j < 999; j++)
{


if
(is_palindrome(i * j) == TRUE)
{

if
((i * j) > last_palindrom)
{


last_palindrom = i * j;
f_no = i;

l_no = j;
}
}
}
}

printf("%d * %d = %d \n", f_no, l_no, last_palindrom);

return
0;
}