Wednesday, January 19, 2011

pattern






/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/*
* two numbers are entered. Both are of 4 digits.
* For each digit there can be three parts
* ( eg for 1234
* 1. 123 and 4
* 2. 12 and 34
* 3. 1 and 234)
*
* We need to find perfect suare number between first number and second number
* where both parts of any one part (of 3 parts) are also perfect square
*
*/



#include<stdio.h>
#define TRUE 1
#define FALSE 0


int
perfectSquare(int n1)
{

long
i;
int
return_val = FALSE;

if
(n1 == 1)
{

return
TRUE;
}


for
(i = 1; i < n1; i++)
{


if
((i * i) == n1)
{

return_val = TRUE;
}
}


return
return_val;
}


void
main()
{

int
n1, n2;

int
i;
int
temp;
int
div = 1000;

printf("\nEnter first number: ");
scanf("%d", &n1);
printf("\nEnter second number: ");

scanf("%d", &n2);
for
(i = n1; i < n2; i++)
{


div = 1000;
while
(div >= 10)
{


temp = i % div;
if
(perfectSquare(temp) == TRUE)
{


temp = i / div;
if
(perfectSquare(temp) == TRUE)
{


if
(1 == perfectSquare(i))
{

printf("%d ", i);
}
}
}


div = div / 10;
}
}

printf("\n");

}

No comments:

Post a Comment