Sunday, January 1, 2012

Print following pattern. 1 2 3 4 8 7 6 5 9 10 11 12 in 'C'


 1 2 3 4
 8 7 6 5
 9 10 11 12

Example



#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,k=1;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=4;j++)
{
if(i%2==0)
{
printf("%4d",k--);
}
else
{
printf("%4d",k++);
}
}
if(i%2==0)
{
k++;
}
else
k--;
k+=4;
printf("\n");
}
getch();
}
/*================================================================
OUTPUT
Enter the number:3
1 2 3 4
8 7 6 5
9 10 11 12
*/

No comments:

Post a Comment