Program for different patterns
1. 1 A
2 A M
3 A M E
4 A M E R
5 A M E R I
6 A M E R I C
7 A M E R I C A
#source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,k;
char a[100]="AMERICA";
k=0;
for(i=0; i<7; i++)
{
printf("%d\t",++k);
for(j=0; j<i; j++)
{
printf("%c\t",a[j]);
}
printf("\n");
}
getch();
}
2. C
H H
I I I
N N N
A A A A
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j;
char a[100]="CHINA";
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%c\t",a[i]);
}
printf("\n");
}
getch();
}
Output:
3.
C
C H
C H I
C H I N
C H I N A
source code
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j;
char a[100]="CHINA";
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%c\t",a[j]);
}
printf("\n");
}
getch();
}
Output:
4. Using nested loop
*
* *
* * *
* * * *
* * * * *
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*\t");
}
printf("\n");
}
getch();
}
Output:
5.
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=6 ;j++)
{
printf("*\t");
}
printf("\n");
}
getch();
}
Output:
6.
* * * * *
* * * *
* * *
* *
*
source code :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("*\t");
}
printf("\n");
}
getch();
}
Output:
7.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}
Output:
8.
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
#SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}
Output:
9.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",j);
}
printf("\n");
}
getch();
}
Output:
10.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((i+j)%2==0)
{
printf("1\t");
}
else
{
printf("0\t");
}
}
printf("\n");
}
getch();
}
Output:
11.
1 2 3 3 1
4 5 6 5 4
7 8 9 8 7
10 11 12 11 10
13 14 15 14 13
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k=0,l;
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",++k);
}
l=k;
for(j=4;j<=5;j++)
{
printf("%d\t",--l);
}
printf("\n");
}
getch();
}
Output:
13.
0 1 1 1 1 1
1 0 1 1 1 1
1 1 0 1 1 1
1 1 1 0 1 1
1 1 1 1 0 1
1 1 1 1 1 0
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=1;j<=6;j++)
{
if(i==j)
{
printf("0\t");
}
else
{
printf("1\t");
}
}
printf("\n");
}
getch();
}
Output:
14.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, space, rows, k=0;
printf("Enter number of rows\n");
scanf("%d", &rows);
for (i=1; i<=rows; ++i,k=0)
{
for (space=1; space<=rows-i; ++space)
{
printf(" ");
}
while (k!=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
}
Output:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=6 ;j++)
{
printf("*\t");
}
printf("\n");
}
getch();
}
Output:
6.
* * * * *
* * * *
* * *
* *
*
source code :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("*\t");
}
printf("\n");
}
getch();
}
Output:
7.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}
Output:
8.
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
#SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}
Output:
9.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",j);
}
printf("\n");
}
getch();
}
Output:
10.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((i+j)%2==0)
{
printf("1\t");
}
else
{
printf("0\t");
}
}
printf("\n");
}
getch();
}
Output:
11.
1 2 3 3 1
4 5 6 5 4
7 8 9 8 7
10 11 12 11 10
13 14 15 14 13
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k=0,l;
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",++k);
}
l=k;
for(j=4;j<=5;j++)
{
printf("%d\t",--l);
}
printf("\n");
}
getch();
}
Output:
13.
0 1 1 1 1 1
1 0 1 1 1 1
1 1 0 1 1 1
1 1 1 0 1 1
1 1 1 1 0 1
1 1 1 1 1 0
source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=1;j<=6;j++)
{
if(i==j)
{
printf("0\t");
}
else
{
printf("1\t");
}
}
printf("\n");
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, space, rows, k=0;
printf("Enter number of rows\n");
scanf("%d", &rows);
for (i=1; i<=rows; ++i,k=0)
{
for (space=1; space<=rows-i; ++space)
{
printf(" ");
}
while (k!=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
}
Output:
No comments :
Post a Comment