Tuesday, December 28, 2010

pattern without array






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

/**
* program for pattern
*/

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

int
getno(int i, int j);
int
find_max(int);

void
main()
{

int
i, j, flag = 1;

int
space;
int
leave_sp;
int
k = 0, l;

printf("Enter no: ");
scanf("%d", &n);
space = find_max(n * n);

leave_sp = (n - 1);
for
(i = 1; i <= ((n - 1)*2) + 1; i++)
{


printf("\n");
for
(k = 0; k < leave_sp; k++)
{


for
(l = 0; l < space; l++)
{


printf(" ");
}
}

for
(j = 1; j <= flag && flag > 0; j++)
{


//printf("%0*d ",-space-1,getno(i,j) );
printf("%*d ", -space - 1, getno(i, j));
}


if
(i >= n)
{

leave_sp++;
flag--;
}
else

{

leave_sp--;
flag++;
}
}

//gethc();
//return 0;
printf("\n");
}


int
getno(int i, int j)
{

int
ti = 0, tj = 0, count = 0, temp;

void
checker(void);
int
main_;
int
br = FALSE;

main_ = n;
temp = n;
count = 0;

br = FALSE;
while
(i != ti && j != tj)
{


if
(br == TRUE)
{

break
;
}

while
(count < temp)
{


ti++;
tj++;
count++;
if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

if
(br == TRUE)
{


continue
;
}

main_--;
temp += main_;
while
(count < temp)
{


ti++;
tj--;
count++;
if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

if
(br == TRUE)
{


continue
;
}

temp += ((main_ - 1)*2) + 1;

while
(count < temp)
{

ti--;
count++;

if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

main_--;
if
(br == TRUE)
{


continue
;
}

temp += (main_);

}

return
count;
}


int
find_max(int a)
{

int
i = 0;

while
(a > 0)
{

a /= 10;

i++;
}

return
i;
}

No comments:

Post a Comment