Pattern Program in C
Pattern Program in C
Pattern Program in C
/*
*
* *
* * *
* * * *
* * * * *
*/
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,row;
clrscr();
printf("Enter the number of rows");
scanf("%d",&row);
for(i=1;i<=row;i++) // for-1(outer) used to control row
{
for(j=1;j<=i;j++) // for-2(inner) used
{
printf("*"); // to print ‘*’
}
printf ("\n");
}
}
/* OUTPUT:
Enter the number of rows 5
*
* *
* * *
* * * *
* * * * *
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row;
clrscr();
printf("Enter the number of rows");
scanf("%d",&row);
for(i=1;i<=row;i++) // for-1(outer) used to control row
{
for(j=1;j<=(row-i);j++)
printf(" "); // to print blank space
for(j =1;j<=i;j++) // for-2(inner) used
printf("*"); // to print ‘*’
printf ("\n");
}
}
/*
OUTPUT:
Enter the number of rows 5
*
**
***
****
*****
*/
/*
*
* *
* * *
* * * *
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,row;
clrscr();
printf("enter row:");
scanf("%d",&row);
for(i=1;i<=row;i++) // line-1
{
for(j=1;j<=(row-i);j++) // line-2
printf(" "); // line-3
for(k=1;k<=i;k++) // line-4
printf("* "); // line-5
printf("\n"); // line-6
}
getch();
}
/* OUTPUT:
enter row 4
*
* *
* * *
* * * *
*/
/* Program 7.33: Write a C program to print the following pattern (till n rows, where n is taken as
input).
/*
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,p;
clrscr();
printf("Enter Number of rows ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\t");
for(j=1;j<=(n-i);j++)
printf(" ");
p=i;
for(j=1;j<2*i;j++)
{
printf("%3d",p);
if(j>=i)
p--;
else
p++;
}
printf("\n\n");
}
getch();
}
/*
N = 5 i Char Blank
------------------
ABCDEEDCBA 1 A-E 0 Blank=2*(i-1)
ABCD..DCBA 2 A-D 2
ABC....CBA 3 A-C 4 Char={0 to (n-i)}+65
AB......BA 4 A-B 6
A........A 5 A-A 8
------------------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=0;j<=(n-i);j++)
{
printf("%c",j+65);
}
for(j=1;j<=2*(i-1);j++)
{
printf(".");
}
for(j=(n-i);j>=0;j--)
{
printf("%c",j+65);
}
printf("\n");
}
printf("\n\n\t\tEnd of the program....");
getch();
}
ABCDEEDCBA
ABCD .. DCBA
ABC .... CBA
AB ...... BA
A ........ A
/*
i b *
-----------
**** n=4 1 0 4
.*** 2 1 3 * => n-(i-1)
..** 3 2 2 b => i-1
...* 4 3 1
-----------
*/
#include <stdio.h>
#include<conio.h>
void main(void)
{
int n,i,j;
clrscr();
printf("\n\n\t\tPlease enter the number of terms....");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
printf(".");
}
for(j=1;j<=(n-(i-1));j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/* 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,r,ncr=1;
clrscr();
for(i=0;i<r;i++)
{
for(j=1;j<(r-i);j++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0 || i==0)
ncr=1;
else
ncr=ncr*(i-j+1)/j;
printf("%6d",ncr);
}
printf("\n\n");
}
getch();
}
1 1
1 2 1
1 3 3 1
1 4 6 4 1
/*
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,c=0;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n ");
for(j=1;j<=i;j++)
{
c=c+1;
if(c%2==0)
printf("0");
else
printf("1");
}
}
getch();
}
O/P->
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
/* Write a C program to illustrate Tower of Hannoi. */
#include<stdio.h>
O/P->
Enter number of disk 3
n = 11 (odd) m = (n+1)/2 = 6
i b *
----------
.....* 1 5 1
....*** 2 4 3 b => m-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** __6__0_11__
.********* 7 1 9
..******* 8 2 7 b => i-m
...***** 9 3 5 * => 2*(n-i)+1
....*** 10 4 3
.....* 11 5 1
----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,b,s,m;
clrscr();
printf("\n\n\t\tPlease enter the number of layers....");
scanf("%d",&n);
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i j
------------------
1 1-10,1 => 10
3 1-10,3 => 4 count = 20 i = 11 j = 19
5 1-10,5 => 2
7 1-10,7 => 2
9 1-10,9 => 2
------------------
*/
/*
n = 11 (odd) i b * m = (n+1)/2 = 6
-----------
*********** 1 0 11
.********* 2 1 9 b => i-1
..******* 3 2 7 * => 2*(m-i)+1
...***** 4 3 5
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b => n-i
..******* 9 2 7 * => 2*(i-m)+1
.********* 10 1 9
*********** 11 0 11
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int s,b,m,n,i,j;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=n-i;
s=2*(i-m)+1;
}
else
{
b=i-1;
s=2*(m-i)+1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i j
--------------
1 1-3,2 => 2
2 1-6,4 => 2
3 1-9,6 => 2
4 1-12,8 => 2
5 1-15,10 => 2
-------------- ----
10 6 21
*/
/*
i *
--------
* n=5 1 1
** 2 2 * => i
*** 3 3
**** 4 4
***** 5 5
--------
*/
#include <stdio.h>
void main(void)
{
int n,i,j;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i b *
-----------
********* n=5 1 0 9
.******* 2 1 7 * => 2*(n-i)+1
..***** 3 2 5 b => i-1
...*** 4 3 3
....* 5 4 1
-----------
*/
#include <stdio.h>
void main(void)
{
int n,i,j;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
printf(".");
}
for(j=1;j<=2*(n-i)+1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i * m = (n+1)/2 = 5
--------
* n=9 1 1
** (odd) 2 2 * => i
*** 3 3
**** 4 4
***** __5__5__
**** 6 4
*** 7 3 * => (n-i)+1
** 8 2
* 9 1
--------
*/
#include <stdio.h>
void main(void)
{
int s,m,n,i,j;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m) s=(n-i)+1; else s=i;
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i b * m = (n+1)/2 = 5
-----------
....* n=9 1 4 1
...*** (odd) 2 3 3 * => 2*i-1
..***** 3 2 5 b => m-i
.******* 4 1 7
********* __5__0__9__
.******* 6 1 7
..***** 7 2 5 * => 2*(n-i)+1
...*** 8 3 3 b => i-m
....* 9 4 1
-----------
*/
#include <stdio.h>
void main(void)
{
int b,s,m,n,i,j;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i<=m)
{
s=2*i-1;
b=m-i;
}
else
{
s=2*(n-i)+1;
b=i-m;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
n=6 i b *
---------
.....* 1 5 1
....*** 2 4 3 b => n-i
...***** 3 3 5
..******* 4 2 7 * => 2*i-1
.********* 5 1 9
*********** 6 0 11
---------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,offset;
clrscr();
offset=(80-(2*n-1))/2;
for(i=1;i<=n;i++)
{
for(j=1;j<=offset;j++)
{
printf(" ");
}
for(j=1;j<=(n-i);j++)
{
printf(".");
}
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
i b *
-----------
......* 1 6 1
.....*** 2 5 3 b => m-i
....***** 3 4 5
...******* 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* _7___0__13_
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,m,s,b;
char ch;
clrscr();
do
{
printf("\n\n\t\tPlease enter the odd number of layers....");
scanf("%d",&n);
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i<=m)
{
b=m-i;
s=2*i-1;
}
else
{
b=i-m;
s=2*(n-i)+1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/* i # *. n = 5
--------
*.*.*.*.*. 1 0 5
#*.*.*.*. 2 1 4 # => i-1
##*.*.*. 3 2 3
###*.*. 4 3 2 *. => n-i+1
####*. 5 4 1
--------
*******
*****
***
*
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int sum=0,i,n,j;
clrscr();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=(i-1);j++)
{
printf(" ");
}
for(j=1;j<=(n-i+1);j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
/*
n = 6 i b *
-----------
.....* 1 5 1
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7 b => n-i
.********* 5 1 9
*********** 6 0 11
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int n,i,j;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
n=6 i *
----------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
n=6 i *
----------
A 1 1
AB 2 2
ABC 3 3 * => i
ABCD 4 4
ABCDE 5 5
ABCDEF 6 6
----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
printf("\n");
}
getch();
}
/*
n=6 i b *
-------------
.....* 1 5 1
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7 b => n-i
.********* 5 1 9
*********** 6 0 11
-------------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,s,b,m;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if(i>m)
{
s=2*(n-i)+1;
b=i-m;
}
else
{
s=2*i-1;
b=m-i;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,s,b,m;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if(i>m)
{
s=2*(i-m)+1;
b=n-i;
}
else
{
s=2*(m-i)+1;
b=i-1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,s,b,m;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i==1 || i==n)
{
s=n;
b=0;
}
else
{
if(i>m)
{
s=i-m+1;
b=2*(n-i)-1;
}
else
{
s=m-i+1;
b=2*i-3;
}
}
for(j=1;j<=s;j++)
{
printf("*.");
}
for(j=1;j<=b;j++)
{
printf(".");
}
printf("\n");
}
getch();
}
n=6 i *
----------
A 1 1
BB 2 2
CCC 3 3 * => i
DDDD 4 4
EEEEE 5 5
FFFFFF 6 6
----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",i+64);
}
printf("\n");
}
getch();
}
/*
n = 13 m = (n+1)/2 = 7
i b *
-------------
......A 1 6 1
.....BBB 2 5 3 b => m-i
....CCCCC 3 4 5
...DDDDDDD 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* __7___0__13__
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-------------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int s,b,n,i,j,m;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}
for(j=1;j<=b;j++)
printf(".");
for(j=1;j<=s;j++)
printf("%c",i+64);
printf("\n");
}
getch();
}
/*
n = 13 m = (n+1)/2 = 7
i b *
-------------
......A 1 6 1
.....BBB 2 5 3 b => m-i
....CCCCC 3 4 5
...DDDDDDD 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* __7___0__13__
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-------------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int ch,s,b,n,i,j,m;
clrscr();
m=(n+1)/2;
ch=64;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}
for(j=1;j<=b;j++)
printf(".");
ch=ch-(i>m)+(i<=m);
for(j=1;j<=s;j++)
printf("%c",ch);
printf("\n");
}
getch();
}
/*
n = 6
i *
--------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
--------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
integer i,j
write(*,*)'Doctor visit'
do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue
100 continue
*/
/*
n = 6
i *
--------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
--------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,m,d;
clrscr();
printf("\n\t\tDoctor's visit....");
for(i=1;i<=7;i++)
{
printf("\n\t\tGood morning....");
for(j=1;j<=3;j++)
{
printf("\n\t\tDay number = %d and medicine number =
%d....",i,j);
}
printf("\n\t\tGood night....");
}
/*
integer i,j
write(*,*)'Doctor visit'
do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue
100 continue
*/
/*
n = 6
i b *
-----------
.....* 1 5 1
....*** 2 4 3 b => n-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** 6 0 11
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
integer i,j
write(*,*)'Doctor visit'
do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue
100 continue
*/
/*
n = 11 (odd) m=(n+1)/2=6
i b *
-----------
.....* 1 5 1
....*** 2 4 3 b => m-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** __6__0_11__
.********* 7 1 9
..******* 8 2 7 b => i-m
...***** 9 3 5 * => 2*(n-i)+1
....*** 10 4 3
.....* 11 5 1
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,m,b,s;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
s=2*(n-i)+1;
b=i-m;
}
else
{
s=2*i-1;
b=m-i;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
/*
integer i,j
write(*,*)'Doctor visit'
do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue
100 continue
*/
/*
n = 11 (odd) m=(n+1)/2=6
i b *
-----------
*********** 1 0 11
.********* 2 1 9 b => i-1
..******* 3 2 7 * =>2*m-(2*i-1)
...***** 4 3 5
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b =>n-i
..******* 9 2 7 * =>2*i-n
*********** 11 0 11
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,m,b,s;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
s=2*i-n;
b=n-i;
}
else
{
s=2*m-(2*i-1);
b=i-1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
integer i,j
write(*,*)'Doctor visit'
do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue
100 continue
*/
/* n = 11 (odd levels) i b *
-----------
*********** 1 0 11
.********* m = (n+1)/2 2 1 9 b => i-1
..******* = 6 3 2 7
...***** 4 3 5 * => 2*(m-i)+1
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b => n-i
..******* 9 2 7
.********* 10 1 9 * => 2*(i-m)+1
*********** 11 0 11
-----------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,m,n,s,b;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=n-i;
s=2*(i-m)+1;
}
else
{
b=i-1;
s=2*(m-i)+1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
n = 15 (odd) m = (n+1)/2 = 8
i b *
-------------
.......* 1 7 1
......*** 2 6 3
.....***** 3 5 5 b => m-i
....******* 4 4 7
...********* 5 3 9 * => 2*i-1
..*********** 6 2 11
.************* 7 1 13
*************** __8___0__15__
.************* 9 1 13
..*********** 10 2 11 b => i-m
...********* 11 3 9
....******* 12 4 7 * => 2*(n-i)+1
.....***** 13 5 5
......*** 14 6 3
.......* 15 7 1
-------------
*/
#include <stdio.h>
#include <conio.h>
main()
{
int i,j,n,m,s,b;
clrscr();
do
{
printf("\n\n\t\tPlease enter the odd number of
layers....");
scanf("%d",&n);
}while(n%2==0);
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}
for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}
printf("\n");
}
getch();
return(0);
}
/*
m = (n+1)/2 = 6
n = 11 (odd) i * b *
-----------
*********** 1 11 0 0 b => 2*(i-1)-1 (except i = 1)
*****.***** 2 5 1 5
****...**** 3 4 3 4 * => m-i+1
***.....*** 4 3 5 3
**.......** 5 2 7 2
*.........* _6__1__9__1_
**.......** 7 2 7 2 b => 2*(n-i)-1 (except i = 11)
***.....*** 8 3 5 3
****...**** 9 4 3 4 * => i-m+1
*****.***** 10 5 1 5
*********** 11 11 0 0
------------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n,m,s,b;
clrscr();
m=(n+1)/2;
for(i=1;i<=n;i++)
{
if (i==1 || i==n)
{
b=0;
s=n;
}
else
{
if (i>m)
{
b=2*(n-i)-1;
s=i-m+1;
}
else
{
b=2*(i-1)-1;
s=m-i+1;
}
}
for(j=1;j<=s;j++)
{
printf("*");
}
for(j=1;j<=b;j++)
{
printf(".");
}
printf("\n");
}
getch();
}
/*
i j
----
1 1-10,1 = 10
3 3-10,3 = 3
5 5-10,5 = 2
---
15 i = 7 j = 15
*/
/*
n = 6 i b *
---------
.....* 1 5 1 b => n-i
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** 6 0 11
---------
************
.**********
..********
...******
....***
.....*
n = 6 i b *.
---------
.....*. 1 5 1
....*.*. 2 4 2 b => n-i
...*.*.*. 3 3 3 *. => i
..*.*.*.*. 4 2 4
.*.*.*.*.*. 5 1 5
*.*.*.*.*.*. 6 0 6
---------
*/
#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,n;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}
for(j=1;j<=i;j++)
{
printf("*");
if (i!=j)
printf(".");
or
if (i==j)
printf("*");
else
printf("*.");
}
printf("\n");
}
getch();
}
/*
i j
---------- i= 1 to 5
1 1-3 ,1 => 3 j= 1 to i
3 3-9 ,3 => 3 15 times
5 5-15,5 => 3
7 7-21,7 => 3
9 9-27,9 => 3
---------- ---
15
*/
/*
looping in C
1 > for loop
2 > do-while
3 > while-do
*/