Monday, December 27, 2010

pattern

Question: Write a program that generates output shown in left image.


The code is:











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

printf("Enter no of rows: ");
scanf("%d",&n);
for
(i = 0; i<n ;i++)
{


for
(j = i; j >=0;j--)
{


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

printf("\n");
}
}


int
fun(int a , int b )
{

int
sum = 0 ;

int
i = 0 , j = 0;

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


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


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


return
sum;
}
}
}

return
sum;
}

No comments:

Post a Comment