Monday, December 27, 2010

pattern

A program that will generate above pattern:








#include<stdio.h>
// this program works well when the value of rows is less than 20
void main()
{


int
n = 5;
int
i = 0 , j = 0 , k = 0;

int
temp = 0 ;
printf("Enter no of rows: ");

scanf("%d",&n);
temp = n;
for
(i = 0; i<n ;i++)
{


// logic for space
for(k = 0 ; k < temp ; k++ )
{


printf(" ");
}

temp--;
// for printing the actuak number
for(j = 0; j <= i ; j++)
{


printf("%3d ", fun(i,j,n));
}

printf("\n");
}
}


int
fun(int a , int b, int n )
{


int
sum = 0 ;
int
i = 0 , j = 0;

for
(i = n-1 ; i >= 0 ; i--)
{


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


sum++;
if
( i==a && b==j )
{


return
sum;
}
}
}

return
sum;
}

No comments:

Post a Comment