Saturday, May 22, 2010

C Programs on Pattern using Nested Loop

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
#include < stdio.h >
void main ()
{
int i,j,x;
clrscr();
for(i=0;i < 9;i++)
{
if(i > 4)
x=x-1;
else
x=i;
for(j=0;j < =x;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}


Write a program to display the pattern:-
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

#include < stdio.h >
#include < conio.h >
void main()
{
int val,row,q,r,x;
clrscr();
val=1;
q=0;
printf("Enter the number of rows:-");
scanf("%d",&row);
while(q < row)
{
for(x=0;x < =q;x++)
{
if(x==0)
val=1;
else
val=(val*(q-x+1))/x;
printf("%4d",val);
}
printf("\n");
q++;
}
getch();
}

1
0 1
1 0 1
0 1 0 1
n terms
#include < stdio.h >
void main()
{
int i,j,n;
clrscr();
printf("Enter the number of rows:-");
scanf("%d",&n);
for(i=0;i < n;i++)
{
for(j=0;j < i+1;j++)
{
if(((i%2==0)&&(j%2==0))||((i%2!=0)&&(j%2!=0)))
printf("%2d",1);
else
printf("%2d",0);
}
printf("\n");
}
getch();
}

Output will be like

0 0 0 0 0 0
0
0
0
0
0 0 0 0 0 0

#include < stdio.h >

void main()
{

int i,j,row,col;
clrscr();
printf("\nEnter the row and column numbers::");
scanf("%d%d",&row,&col);
if(row!=col)
{
printf("\nEnter row and column numbers same.");
getch();
return;
}
for(i=0;i < row;i++)
{
for(j=0;j < col;j++)
{
if((i==0)||(i==row-1)||(i+j==row-1))
printf("%2c",'0');
else
printf("%2c",' ');
}
printf("\n");
}
getch();
}

pattern of digits

1
1 1
1 2 1
3 3 1
1 4 6 4 1

#include < stdio.h >
#include < conio.h >
void main()
{
int val,row,q,r,x;
clrscr();
val=1;
q=0;
printf("Enter the number of rows:-");
scanf("%d",&row);
while(q < row)
{
for(x=0;x < =q;x++)
{
if(x==0)
val=1;
else
val=(val*(q-x+1))/x;
printf("%4d",val);
}
printf("\n");
q++;
}
getch();
}

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

#include < stdio.h >
#include < conio.h >
void main()
{
int p,m,q,n;
clrscr();
printf("Enter the number of rows:-");
scanf("%d",&n);
for(p=1;p < =n;p++)
{
m=p
for(q=0;q < p;q++)
{
printf("%4d",m++);
}
m=m-2;
for(q=1;q < p;q++)
{
printf("%4d",m--);
}
printf("\n");
}
getch();
}

pattern of alphabets


A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

#include < stdio.h >
void main()
{
int a,i,j,m,k,x=1;
clrscr();
for(i=0;i < 4;i++)
{
a=65;
for(j=i;j < 4;j++)
{
printf("%2c",a);
a++;
}
a=a-1;
if(i!=0)
{
for(k=0;k < x+i;k++)
{
printf("%2c",' ');
}
x=x+1;
}
for(m=i;m < 4;m++)
{
printf("%2c",a);
a=a-1;
}
printf("\n");
}
getch();
}

1 comment:

Subscribe via email

Enter your email address:

Delivered by FeedBurner