Saturday, January 31, 2015

Pattern in c

A tricky pattern in cprogram

given
 n=4
values:
1
2
3
4
k=8

output :
11112222
11112222
11112222
11112222
33334444
33334444
33334444
33334444


given

n=9

values:
1
2
3
4
5
6'
7
8
9

k=9

output

111222333
111222333
111222333
444555666
444555666
444555666
777888999
777888999
777888999



given
n=16
values


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

k=1

output:
1   2     3     4
5   6     7     8
9    10 11   12
13 14  15   16



program:




#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,m,jj,times,j,k,l,a[100];
clrscr();
printf("Enter value of n:");
scanf("%d",&n);
printf("enter the array elements:"):
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the value of k:");
scanf("%d",&k);
printf("\n\n\n\nThe pattern is\n");
times=sqrt((k*k)/n);
jj=times;
if(times!=1)
{
for(i=0;i<n;i+=(k/times))
{
for(m=0;m<times;m++)
{
for(j=0,jj=times,l=0;j<k;j++)
{
printf("%d ",a[i+l]);
if(j==(jj-1))
{
l++;
jj+=times;
}
}
printf("\n");
}
}
}

else
{
l=k;
for(i=0;i<n;i++)
{
printf("%d   ",a[i]);
if(i==(k-1))
{
printf("\n");
k+=l;
}
}
}
getch();

}

No comments:

Post a Comment